# Stderr stdout redirection with Ruby?

**URL:** <https://www.ruby-forum.com/t/stderr-stdout-redirection-with-ruby/111742>\
**Category:** Ruby\
**Created:** [August 10, 2007, 11:32am UTC](https://www.ruby-forum.com/t/stderr-stdout-redirection-with-ruby/111742 "2007-08-10T11:32:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![unbewust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/9d8465/32.png) [@unbewust](https://www.ruby-forum.com/u/unbewust)\
**Post date:** [August 10, 2007, 11:32am UTC](https://www.ruby-forum.com/t/stderr-stdout-redirection-with-ruby/111742/1 "2007-08-10T11:32:02Z")

</div>

i do have a shell script doing :

`man "#{arg}" 2> "#{tmp}/#{fm}" PIPE man2html > "#{tmp}/#{f1}"`

because i want to know if there is “No manual entry for “#{arg}””

then for the time being i’m using an tmp file “#{tmp}/#{fm}” which i  
read after to know i i get the message :

“No manual entry for “#{arg}””

i’m sure there is a more elegant way doing that in Ruby, avoiding  
shelling, BUT HOW TO ?

---

<div class="post-metadata">

**Author:** ![unbewust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/9d8465/32.png) [@unbewust](https://www.ruby-forum.com/u/unbewust)\
**Post date:** [August 10, 2007, 1:27pm UTC](https://www.ruby-forum.com/t/stderr-stdout-redirection-with-ruby/111742/2 "2007-08-10T13:27:24Z")

</div>

> i’m sure there is a more elegant way doing that in Ruby, avoiding  
> shelling, BUT HOW TO ?

If you want to do it completely within Ruby, you could first slurp the  
output of man into a Ruby variable, i.e.

man\_page=%x(man #{arg})

and if it is OK, i.e.

if man\_page.length \> 0  
…  
send this as stdin into man2html, i.e. something like:

to\_html=IO.popen(“man2html \>#{f1}”,“w”)  
to\_html.print(man\_page)  
to\_html.close

From a logical point of view, this solution has the advantage  
that you don’t overwrite your file f1 if the man page does not exist.

For a simpler solution (a bit dirty, but less keystrokes), you might  
consider  
the following idea, which however _does_ use a shell:

error\_message=%x[(man #{arg}|man2html \>#{f1}) 2\>&1]

After this, error\_message contains whatever man and/org man2html spilled  
out onto stderr, but f1 is always overwritten (even if there is no man  
page).

HTH,

Ronald

---

<div class="post-metadata">

**Author:** ![unbewust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/9d8465/32.png) [@unbewust](https://www.ruby-forum.com/u/unbewust)\
**Post date:** [August 10, 2007, 9:41pm UTC](https://www.ruby-forum.com/t/stderr-stdout-redirection-with-ruby/111742/3 "2007-08-10T21:41:27Z")

</div>

On Friday 10 August 2007 04:26:00 am Ronald F. wrote:

> > 
> 
> if man\_page.length \> 0  
> For a simpler solution (a bit dirty, but less keystrokes), you might
> 
> Ronald

By using ‘man\_page=%x(man #{arg})’ you’re shelling out as well. It’s the  
same as writing 'man\_page = `man #{arg}`'. However, it’s probably good  
to  
do the checking / conversion stuff inside ruby and shell out multiple  
times  
rather than having actual program flow / code in sh scripting :D.

---

<div class="post-metadata">

**Author:** ![unbewust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/9d8465/32.png) [@unbewust](https://www.ruby-forum.com/u/unbewust)\
**Post date:** [August 10, 2007, 3:06pm UTC](https://www.ruby-forum.com/t/stderr-stdout-redirection-with-ruby/111742/4 "2007-08-10T15:06:41Z")

</div>

On 10 août, 13:26, “Ronald F.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> if man\_page.length \> 0  
> For a simpler solution (a bit dirty, but less keystrokes), you might  
> consider  
> the following idea, which however _does_ use a shell:
> 
> error\_message=%x[(man #{arg}|man2html \>#{f1}) 2\>&1]
> 
> After this, error\_message contains whatever man and/org man2html spilled  
> out onto stderr, but f1 is always overwritten (even if there is no man  
> page).

OK, fine thanks, in fact f1 is never overwritten the way i use it …

thanks a lot !

---

<div class="post-metadata">

**Author:** ![unbewust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/9d8465/32.png) [@unbewust](https://www.ruby-forum.com/u/unbewust)\
**Post date:** [August 10, 2007, 11:03pm UTC](https://www.ruby-forum.com/t/stderr-stdout-redirection-with-ruby/111742/5 "2007-08-10T23:03:37Z")

</div>

On Aug 10, 2007, at 3:30 AM, unbewust wrote:

> i’m sure there is a more elegant way doing that in Ruby, avoiding  
> shelling, BUT HOW TO ?

> 

require ‘rubygems’  
require ‘open4’

stdin = ‘’, stdout = ‘’, stderr = ‘’

Open4.spawn cmd, :stdin=\>stdin, :stdout=\>stdout, :stderr=\>stderr

there are many example in the dist and postings on this list.

a @ [http://drawohara.com/](http://drawohara.com/)

---

<div class="post-metadata">

**Author:** ![unbewust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/9d8465/32.png) [@unbewust](https://www.ruby-forum.com/u/unbewust)\
**Post date:** [August 11, 2007, 12:44pm UTC](https://www.ruby-forum.com/t/stderr-stdout-redirection-with-ruby/111742/6 "2007-08-11T12:44:28Z")

</div>

On 10 août, 23:02, “ara.t.howard” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> stdin = ‘’, stdout = ‘’, stderr = ‘’
> 
> Open4.spawn cmd, :stdin=\>stdin, :stdout=\>stdout, :stderr=\>stderr
> 
> there are many example in the dist and postings on this list.
> 
> a @[http://drawohara.com/](http://drawohara.com/)

Fine ! thanks a lot !!!

---

<div class="post-metadata">

**Author:** ![unbewust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/9d8465/32.png) [@unbewust](https://www.ruby-forum.com/u/unbewust)\
**Post date:** [August 11, 2007, 3:25pm UTC](https://www.ruby-forum.com/t/stderr-stdout-redirection-with-ruby/111742/7 "2007-08-11T15:25:42Z")

</div>

On 10 août, 23:02, “ara.t.howard” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> stdin = ‘’, stdout = ‘’, stderr = ‘’
> 
> Open4.spawn cmd, :stdin=\>stdin, :stdout=\>stdout, :stderr=\>stderr

i’ve tested allready, unfortunately i get a " undefined method spawn  
for main object" after having included it…

Yvon

---

<div class="post-metadata">

**Author:** ![unbewust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/9d8465/32.png) [@unbewust](https://www.ruby-forum.com/u/unbewust)\
**Post date:** [August 11, 2007, 5:28pm UTC](https://www.ruby-forum.com/t/stderr-stdout-redirection-with-ruby/111742/8 "2007-08-11T17:28:03Z")

</div>

On 8/11/07, unbewust [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > require ‘open4’
> > 
> > stdin = ‘’, stdout = ‘’, stderr = ‘’
> > 
> > Open4.spawn cmd, :stdin=\>stdin, :stdout=\>stdout, :stderr=\>stderr
> 
> i’ve tested allready, unfortunately i get a " undefined method spawn  
> for main object" after having included it…

Did you do this?  
include Open4

spawn cmd, …

Because that’s not going to work

spawn is a singleton method of the Open4 module.

Yvon
