From: Tim Hunter Date: 2007-12-27T12:03:40+09:00 Subject: Re: Capturing error msgs from output = `cmd` Victor Reyes wrote: > Hello Team, > > I wrote the simple piece of code below which under "normal" circumstances it > works fine. > If, however, the command executed generates an error msg, the error is > displayed on the screen. > This is a behaviour which I don't want. > I would like to capture ALL output generated by the command and return it to > the caller. > I tried different tricks but nothing worked. The question is: > > How can I capture ALL output from line: *userCMD_output = `#{input}`* > including stderr??? > When you use backticks to capture the output from a command, what you get is whatever the command writes to stdout. If the command writes an error message to stderr you can't capture it. Check out the open3 library and the popen3 method instead. $ ri Open3#popen3 ----------------------------------------------------------- Open3#popen3 popen3(*cmd) {|| ...} ------------------------------------------------------------------------ Open stdin, stdout, and stderr streams and start external executable. Non-block form: require 'open3' [stdin, stdout, stderr] = Open3.popen3(cmd) Block form: require 'open3' Open3.popen3(cmd) { |stdin, stdout, stderr| ... } The parameter cmd is passed directly to Kernel#exec. -- RMagick: http://rmagick.rubyforge.org/