当我做类似如下:获取系统调用的错误输出?
output = `identify some_file`
output == "Output of identify"
但是,当...
output = `identify non_existant_file`
output != "Error output of identify"
我怎样才能得到系统调用的错误输出?
当我做类似如下:获取系统调用的错误输出?
output = `identify some_file`
output == "Output of identify"
但是,当...
output = `identify non_existant_file`
output != "Error output of identify"
我怎样才能得到系统调用的错误输出?
您可以使用Open3.popen3
。
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/open3/rdoc/Open3.html#method-c-popen3
popen3(* CMD,&块)点击展开源
开放标准输入,stdout和stderr流并启动外部可执行。
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.
}
我找到了答案。输出被发送到stderr。这样我就可以添加以下内容在命令的最后重定向错误输出到标准输出:
output = `identify any_file 2>&1`
output == "Error or output of identify"