# Stderr stdout redirection with Ruby?

**URL:** <https://rubytalk.org/t/stderr-stdout-redirection-with-ruby/39805>\
**Category:** ruby-talk\
**Created:** [10 August 2007 09:30 UTC](https://rubytalk.org/t/stderr-stdout-redirection-with-ruby/39805 "2007-08-10T09:30:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![unbewusst](https://avatars.discourse-cdn.com/v4/letter/u/ba9def/32.png) [@unbewusst](https://rubytalk.org/u/unbewusst)\
**Post date:** [10 August 2007 09:30 UTC](https://rubytalk.org/t/stderr-stdout-redirection-with-ruby/39805/1 "2007-08-10T09:30:13Z")

</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:** ![Ronald\_Fischer1](https://avatars.discourse-cdn.com/v4/letter/r/c2a13f/32.png) [@Ronald\_Fischer1](https://rubytalk.org/u/Ronald_Fischer1)\
**Post date:** [10 August 2007 11:26 UTC](https://rubytalk.org/t/stderr-stdout-redirection-with-ruby/39805/2 "2007-08-10T11:26:00Z")

</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 ?

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

&nbsp;&nbsp;man\_page=%x(man #{arg})  
&nbsp;&nbsp;  
and if it is OK, i.e.

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

&nbsp;&nbsp;to\_html=IO.popen("man2html \>#{f1}","w")  
&nbsp;&nbsp;to\_html.print(man\_page)  
&nbsp;&nbsp;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:

&nbsp;&nbsp;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

> **···**
>
> --  
> Ronald Fischer \<ronald.fischer@venyon.com\>  
> Phone: +49-89-452133-162

---

<div class="post-metadata">

**Author:** ![a11](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/a11/32/8169_2.png) [@a11](https://rubytalk.org/u/a11)\
**Post date:** [10 August 2007 21:02 UTC](https://rubytalk.org/t/stderr-stdout-redirection-with-ruby/39805/3 "2007-08-10T21:02:44Z")

</div>

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/)

> **···**
>
> 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 ?
> 
> --  
> we can deny everything, except that we have the possibility of being better. simply reflect on that.  
> h.h. the 14th dalai lama

---

<div class="post-metadata">

**Author:** ![unbewusst](https://avatars.discourse-cdn.com/v4/letter/u/ba9def/32.png) [@unbewusst](https://rubytalk.org/u/unbewusst)\
**Post date:** [10 August 2007 13:04 UTC](https://rubytalk.org/t/stderr-stdout-redirection-with-ruby/39805/4 "2007-08-10T13:04:58Z")

</div>

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

thanks a lot !

> **···**
>
> On 10 août, 13:26, "Ronald Fischer" \<ronald.fisc...@venyon.com\> wrote:
> 
> > \> 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 ?
> > 
> > If you want to do it completely within Ruby, you could first slurp the  
> > output of man into a Ruby variable, i.e.
> > 
> > &nbsp;&nbsp;man\_page=%x(man #{arg})
> > 
> > and if it is OK, i.e.
> > 
> > &nbsp;&nbsp;if man\_page.length \> 0  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
> > send this as stdin into man2html, i.e. something like:
> > 
> > &nbsp;&nbsp;to\_html=IO.popen("man2html \>#{f1}","w")  
> > &nbsp;&nbsp;to\_html.print(man\_page)  
> > &nbsp;&nbsp;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:
> > 
> > &nbsp;&nbsp;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).

---

<div class="post-metadata">

**Author:** ![Konrad\_Meyer](https://avatars.discourse-cdn.com/v4/letter/k/df705f/32.png) [@Konrad\_Meyer](https://rubytalk.org/u/Konrad_Meyer)\
**Post date:** [10 August 2007 19:40 UTC](https://rubytalk.org/t/stderr-stdout-redirection-with-ruby/39805/5 "2007-08-10T19:40:43Z")

</div>

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.

> **···**
>
> On Friday 10 August 2007 04:26:00 am Ronald Fischer wrote:
> 
> > \> 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 ?
> > 
> > If you want to do it completely within Ruby, you could first slurp the  
> > output of man into a Ruby variable, i.e.
> > 
> > &nbsp;&nbsp;man\_page=%x(man #{arg})  
> > &nbsp;&nbsp;  
> > and if it is OK, i.e.
> > 
> > &nbsp;&nbsp;if man\_page.length \> 0  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
> > send this as stdin into man2html, i.e. something like:
> > 
> > &nbsp;&nbsp;to\_html=IO.popen("man2html \>#{f1}","w")  
> > &nbsp;&nbsp;to\_html.print(man\_page)  
> > &nbsp;&nbsp;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:
> > 
> > &nbsp;&nbsp;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
> 
> --  
> Konrad Meyer \<konrad@tylerc.org\> [http://konrad.sobertillnoon.com/](http://konrad.sobertillnoon.com/)

---

<div class="post-metadata">

**Author:** ![unbewusst](https://avatars.discourse-cdn.com/v4/letter/u/ba9def/32.png) [@unbewusst](https://rubytalk.org/u/unbewusst)\
**Post date:** [11 August 2007 10:10 UTC](https://rubytalk.org/t/stderr-stdout-redirection-with-ruby/39805/6 "2007-08-11T10:10:30Z")

</div>

Fine ! thanks a lot !!!

> **···**
>
> On 10 août, 23:02, "ara.t.howard" \<ara.t.how...@gmail.com\> wrote:
> 
> > 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:** ![unbewusst](https://avatars.discourse-cdn.com/v4/letter/u/ba9def/32.png) [@unbewusst](https://rubytalk.org/u/unbewusst)\
**Post date:** [11 August 2007 13:24 UTC](https://rubytalk.org/t/stderr-stdout-redirection-with-ruby/39805/7 "2007-08-11T13:24:59Z")

</div>

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

Yvon

> **···**
>
> On 10 août, 23:02, "ara.t.howard" \<ara.t.how...@gmail.com\> wrote:
> 
> > 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

---

<div class="post-metadata">

**Author:** ![Logan\_Capaldo](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Logan\_Capaldo](https://rubytalk.org/u/Logan_Capaldo)\
**Post date:** [11 August 2007 15:26 UTC](https://rubytalk.org/t/stderr-stdout-redirection-with-ruby/39805/8 "2007-08-11T15:26:44Z")

</div>

> \>  
> \>  
> \>  
> \> \> 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
> 
> 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

> **···**
>
> On 8/11/07, unbewust \<yvon.thoraval@gmail.com\> wrote:
> 
> > On 10 août, 23:02, "ara.t.howard" \<ara.t.how...@gmail.com\> wrote:  
> > \> On Aug 10, 2007, at 3:30 AM, unbewust wrote:
