stderr stdout redirection with Ruby ???

U

unbewust

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

Ronald Fischer

i do have a shell script doing :
=20
`man "#{arg}" 2> "#{tmp}/#{fm}" PIPE man2html > "#{tmp}/#{f1}"`
=20
because i want to know if there is "No manual entry for "#{arg}""
=20
then for the time being i'm using an tmp file "#{tmp}/#{fm}" which i
read after to know i i get the message :
=20
"No manual entry for "#{arg}""
=20
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=3D%x(man #{arg})
=20
and if it is OK, i.e.

if man_page.length > 0
...
send this as stdin into man2html, i.e. something like:

to_html=3DIO.popen("man2html >#{f1}","w")
to_html.print(man_page)
to_html.close

From a logical point of view, this solution has the advantage=20
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=3D%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
--=20
Ronald Fischer <[email protected]>
Phone: +49-89-452133-162
 
U

unbewust

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 !
 
K

Konrad Meyer

--nextPart4280528.1OCBd24pH3
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

i do have a shell script doing :
=20
`man "#{arg}" 2> "#{tmp}/#{fm}" PIPE man2html > "#{tmp}/#{f1}"`
=20
because i want to know if there is "No manual entry for "#{arg}""
=20
then for the time being i'm using an tmp file "#{tmp}/#{fm}" which i
read after to know i i get the message :
=20
"No manual entry for "#{arg}""
=20
i'm sure there is a more elegant way doing that in Ruby, avoiding
shelling, BUT HOW TO ?
=20
If you want to do it completely within Ruby, you could first slurp the
output of man into a Ruby variable, i.e.
=20
man_page=3D%x(man #{arg})
=20
and if it is OK, i.e.
=20
if man_page.length > 0
...
send this as stdin into man2html, i.e. something like:
=20
to_html=3DIO.popen("man2html >#{f1}","w")
to_html.print(man_page)
to_html.close
=20
From a logical point of view, this solution has the advantage=20
that you don't overwrite your file f1 if the man page does not exist.
=20
For a simpler solution (a bit dirty, but less keystrokes), you might
consider
the following idea, which however *does* use a shell:
=20
error_message=3D%x[(man #{arg}|man2html >#{f1}) 2>&1]
=20
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).
=20
HTH,
=20
Ronald

By using 'man_page=3D%x(man #{arg})' you're shelling out as well. It's the
same as writing 'man_page =3D `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.

=2D-=20
Konrad Meyer <[email protected]> http://konrad.sobertillnoon.com/

--nextPart4280528.1OCBd24pH3
Content-Type: application/pgp-signature; name=signature.asc
Content-Description: This is a digitally signed message part.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQBGvL8/CHB0oCiR2cwRAkCtAKCxfuGJHOUpdLwOjKx5AkKhjLtiIQCfbxpB
ild5j9gWjxozCpB0bz7WefE=
=97k1
-----END PGP SIGNATURE-----

--nextPart4280528.1OCBd24pH3--
 
A

ara.t.howard

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

unbewust

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/

Fine ! thanks a lot !!!
 
U

unbewust

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top