Merge stdout and stderr in Popen

三世轮回 提交于 2019-12-23 21:47:54

问题


In Ruby's popen/spawn, how do I merge both STDOUT and STDERR as a single stream wihthout resorting to using >2&1?

In Python, this would be:

>>> import subprocess
>>> subprocess.check_output('my_prog args', stderr=subprocess.STDOUT, shell=True)

Note the stderr argument.

I use Open3 - as I don't want just stdout - but it already separates them into two streams.


回答1:


Using the code from your other question, here you go:

cmd = 'a_prog --arg ... --arg2 ...'
Open3.popen2({"MYVAR" => "a_value"}, "#{cmd}", {:err => [:child, :out]}) { |i,o|
  # This output should include stderr as well
  output = o.read()
  repr = "$ #{cmd}\n#{output}"
}

A couple changes:

  1. The third parameter to popen2 will redirect stderr to stdoutl. Note that it needs to be the spawned process's stdout, not the system-wide stdout, so you need to specify :child's :out
  2. You need to use .popen2 instead of .popen3 as it seems the redirection is ignored if you include the 3rd e option for stderr
  3. Because you're using .popen2, you only pass |i,o| to the block:



回答2:


A bit late, but take a look at Open3.popen2e - docs.

This behaves exactly as popen3, but merges stderr stdout as the second argument to the block.

So you can simply do

cmd = 'a_prog --arg ... --arg2 ...'
Open3.popen2e(cmd) { |input,output|
 # Process as desired, with output containing stdout and stderr
}


来源:https://stackoverflow.com/questions/5917993/merge-stdout-and-stderr-in-popen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!