[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

stderr stdout redirection with Ruby ???

unbewust

8/10/2007 9:28:00 AM


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 ?

6 Answers

Ronald Fischer

8/10/2007 11:26:00 AM

0

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

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
--
Ronald Fischer <ronald.fischer@venyon.com>
Phone: +49-89-452133-162

unbewust

8/10/2007 1:00:00 PM

0

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

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

thanks a lot !

Konrad Meyer

8/10/2007 7:41:00 PM

0

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

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.

--
Konrad Meyer <konrad@tylerc.org> http://konrad.sobertil...

ara.t.howard

8/10/2007 9:03:00 PM

0


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




unbewust

8/11/2007 10:09:00 AM

0

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://draw...

Fine ! thanks a lot !!!

unbewust

8/11/2007 1:22:00 PM

0

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

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

Yvon