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.

On Wed, Feb 16, 2011 at 7:04 PM, Ted F. [email protected]
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?

Two options:

  1. Redirect STDOUT to STDERR in the command:

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

  1. 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