Capturing stderr for a file

Hi everyone,

I am trying to run a script from ruby, but it produces some output to
STDERR. It isn't capturing that output. How do I capture it?

file = File.open("my.txt", "w")

arr.each do |e|
  file.puts `./script.sh #{e}`
end

Ted.

···

--
Posted via http://www.ruby-forum.com/.

Two options:

1. Redirect STDOUT to STDERR in the command:

output = `./script.sh #{e} 2>&1`

2. Use Open3 to capture the error stream:

require 'open3'
Open3.popen3("./script.sh #{e} 2>&1") do |i, o, e|
  puts "STDOUT: #{o.read}"
  puts "STDERR: #{e.read}"
end

···

On Wed, Feb 16, 2011 at 7:04 PM, Ted Flethuseo <flethuseo@gmail.com> wrote:

I am trying to run a script from ruby, but it produces some output to
STDERR. It isn't capturing that output. How do I capture it?