capturing stderr for a file

Discussion in 'Ruby' started by Ted Flethuseo, Feb 17, 2011.

  1. 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/.
     
    Ted Flethuseo, Feb 17, 2011
    #1
    1. Advertising

  2. Ted Flethuseo

    Avdi Grimm Guest

    On Wed, Feb 16, 2011 at 7:04 PM, Ted Flethuseo <> 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`

    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
     
    Avdi Grimm, Feb 17, 2011
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Doru-Catalin Togea

    Capturing output from stderr

    Doru-Catalin Togea, Dec 5, 2005, in forum: Python
    Replies:
    2
    Views:
    402
    Doru-Catalin Togea
    Dec 5, 2005
  2. Fuzzyman
    Replies:
    3
    Views:
    660
    Gabriel Genellina
    Jan 7, 2007
  3. Carl Lerche

    Capturing stderr from Kernel.exec

    Carl Lerche, Sep 9, 2006, in forum: Ruby
    Replies:
    2
    Views:
    135
  4. Brian

    capturing stderr from windows

    Brian, Apr 19, 2004, in forum: Perl Misc
    Replies:
    4
    Views:
    99
    Toni Erdmann
    Apr 22, 2004
  5. Mike

    capturing stdout and stderr?

    Mike, Aug 9, 2009, in forum: Perl Misc
    Replies:
    0
    Views:
    108
Loading...

Share This Page