1

When I do something like the following:

output = `identify some_file`
output == "Output of identify"

But when...

output = `identify non_existant_file`
output != "Error output of identify"

How can I get the error output of system calls?

Zequez
  • 3,399
  • 2
  • 31
  • 42

2 Answers2

5

I found out the answer. The output is being sent to stderr. So I can just add the following at the end of the command to redirect stderr to stdout:

output = `identify any_file 2>&1`
output == "Error or output of identify"

Here is the explanation of this witchcraft

Community
  • 1
  • 1
Zequez
  • 3,399
  • 2
  • 31
  • 42
2

You may use Open3.popen3.

http://www.ruby-doc.org/stdlib-1.9.3/libdoc/open3/rdoc/Open3.html#method-c-popen3

popen3(*cmd, &block) click to toggle source

Open stdin, stdout, and stderr streams and start external executable.

Open3.popen3([env,] cmd... [, opts]) {|stdin, stdout, stderr, wait_thr|
  pid = wait_thr.pid # pid of the started process.
  ...
  exit_status = wait_thr.value # Process::Status object returned.
}
knut
  • 27,320
  • 6
  • 84
  • 112