From: Christophe Broult Date: 2001-10-10T22:33:01+09:00 Subject: [ruby-talk:22383] how do you capture a command executio error like "command not fou nd" This message is in MIME format. Since your mail reader does not understand this format, some or all of this message may not be legible. ------_=_NextPart_001_01C15190.0C601B10 Content-Type: text/plain; charset="iso-8859-1" Hi, I have the following file: how-do-you-capture-cmd-error-output.rb CMD_THAT_WILL_NOT_BE_FOUND = "" cmdOutput = `#{CMD_THAT_WILL_NOT_BE_FOUND}` puts <<-END_OF_MSG __ Command output is __ #{cmdOutput} __ end of command output __ END_OF_MSG Obviously I am going to get an error. Ruby is printing an error message as the following execution shows: ruby -w how-do-you-capture-cmd-error-output.rb how-do-you-capture-cmd-error-output.rb:4: command not found: __ Command output is __ __ end of command output __ My question is how do I prevent Ruby the message from appearing on stderr. Do I have to use fork and capture the command output? Is there anything simpler than the following I adapted from the IO.pipe example in the Pick Axe book: CMD_THAT_WILL_NOT_BE_FOUND = "" rd, wr = IO.pipe if fork wr.close cmdOutput = rd.readlines rd.close Process.wait puts <<-END_OF_MSG __ Command output is __ #{cmdOutput} __ end of command output __ END_OF_MSG else $stderr.reopen(wr) $stdout.reopen(wr) print `#{CMD_THAT_WILL_NOT_BE_FOUND}` rd.close wr.close end Thank you for your time, Christophe ---- Christophe Broult Sapient +1.617.374.0328 One Memorial Drive Cambridge, MA02142 USA ------_=_NextPart_001_01C15190.0C601B10 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable how do you capture a command executio error like "command = not found"

Hi,

I have the following file: = how-do-you-capture-cmd-error-output.rb

CMD_THAT_WILL_NOT_BE_FOUND =3D ""

cmdOutput =3D `#{CMD_THAT_WILL_NOT_BE_FOUND}`
puts <<-END_OF_MSG
__ Command output is __
#{cmdOutput}
__ end of command output __
END_OF_MSG

Obviously I am going to get an error. Ruby is = printing an error message as the following execution shows:

ruby -w how-do-you-capture-cmd-error-output.rb
how-do-you-capture-cmd-error-output.rb:4: command = not found:
__ Command output is __

__ end of command output __
 
My question is how do I prevent Ruby the message = from appearing on stderr. Do I have to use fork
and capture the command output? Is there anything = simpler than the following I adapted from the IO.pipe example
in the Pick Axe book:


CMD_THAT_WILL_NOT_BE_FOUND =3D ""

rd, wr =3D IO.pipe
if fork
  wr.close
  cmdOutput =3D rd.readlines
  rd.close
  Process.wait
  puts <<-END_OF_MSG
__ Command output is __
#{cmdOutput}
__ end of command output __
END_OF_MSG
else
  $stderr.reopen(wr)
  $stdout.reopen(wr)
  print `#{CMD_THAT_WILL_NOT_BE_FOUND}`
  rd.close
  wr.close
end

Thank you for your time,

Christophe

----
Christophe Broult 
Sapient         &n= bsp;           &n= bsp; +1.617.374.0328
One Memorial Drive
Cambridge, MA02142
USA

------_=_NextPart_001_01C15190.0C601B10--