From: Tom Metge Date: 2007-12-27T14:04:43+09:00 Subject: Re: Capturing error msgs from output = `cmd` On Dec 26, 2007, at 9:30 PM, James Gray wrote: > On Dec 26, 2007, at 9:03 PM, Tim Hunter wrote: > >> 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. > > Well, you could fold STDERR into STDOUT. In my Unix shell that's > done with: > > `#{cmd} 2>&1` > > Hope that helps. > > James Edward Gray II > that's a great idea. alternately, you could use open3: http://ruby-doc.org/stdlib/libdoc/open3/rdoc/index.html it does make it a bit easier to do error checking/handling (by differentiating stderr from stdout). you can use nil for the values you don't want... nil, stdout, stderr = popen3("cmd") tom