In Ruby, I want to be able to:
- run a command line (via shell)
- capture both stdout and stderr (preferably as single stream) without using
>2&1
(which fails for some commands here) - run with additional enviornment variables (without modifying the environment of the ruby program itself)
I learned that Open3
allows me to do 1 and 2.
cmd = 'a_prog --arg ... --arg2 ...'
Open3.popen3("#{cmd}") { |i,o,e|
output = o.read()
error = e.read()
# FIXME: don't want to *separate out* stderr like this
repr = "$ #{cmd}\n#{output}"
}
I also learned that popen allows you to pass an environment but not when specifying the commandline.
How do I write code that does all the three?
...
Put differently, what is the Ruby equivalent of the following Python code?
>>> import os, subprocess
>>> env = os.environ.copy()
>>> env['MYVAR'] = 'a_value'
>>> subprocess.check_output('ls -l /notexist', env=env, stderr=subprocess.STDOUT, shell=True)