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.
Using the code from your other question, here you go:
A couple changes:
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
.popen2
instead of.popen3
as it seems the redirection is ignored if you include the 3rde
option for stderr.popen2
, you only pass|i,o|
to the block:A bit late, but take a look at
Open3.popen2e
- docs.This behaves exactly as
popen3
, but mergesstderr
stdout
as the second argument to the block.So you can simply do