How to separate stdout & stderr from executed shell command

Discussion in 'Ruby' started by jqwoods@gmail.com, Mar 14, 2008.

  1. Guest

    I would like to create a method which executes a given shell command
    and returns an array containing three things: 1) stdout of the command
    as a string, 2) stderr of the command as a string, and 3) the exit
    status. I'm having trouble figuring out how to get stdout and stderr
    as separate output.

    Here's an illustration of the desired result I'm after:

    irb> my_exec_command("ls existant_file")
    => ["existant_file\n", "", 0]

    irb> my_exec_command("ls NON_existant_file")
    => ["", "ls: NON_existant_file: No such file or directory\n", 2]

    Any pointers on how to implement my_exec_command?
    , Mar 14, 2008
    #1
    1. Advertising

  2. Guest

    On Mar 14, 11:40 am, wrote:
    > I would like to create a method which executes a given shell command
    > and returns an array containing three things: 1) stdout of the command
    > as a string, 2) stderr of the command as a string, and 3) the exit
    > status. I'm having trouble figuring out how to get stdout and stderr
    > as separate output.
    >
    > Here's an illustration of the desired result I'm after:
    >
    > irb> my_exec_command("ls existant_file")
    > => ["existant_file\n", "", 0]
    >
    > irb> my_exec_command("ls NON_existant_file")
    > => ["", "ls: NON_existant_file: No such file or directory\n", 2]
    >
    > Any pointers on how to implement my_exec_command?


    PS: Unless there's a simpler way, if someone could just point me to an
    API that provides stdout and stderr of sub-processes as IO objects, I
    could probably figure it out from there. Thanks!
    , Mar 14, 2008
    #2
    1. Advertising

  3. Re: How to separate stdout & stderr from executed shell comm

    unknown wrote:
    > On Mar 14, 11:40 am, wrote:
    >>
    >> irb> my_exec_command("ls NON_existant_file")
    >> => ["", "ls: NON_existant_file: No such file or directory\n", 2]
    >>
    >> Any pointers on how to implement my_exec_command?

    >
    > PS: Unless there's a simpler way, if someone could just point me to an
    > API that provides stdout and stderr of sub-processes as IO objects, I
    > could probably figure it out from there. Thanks!


    Take a look at the IO class.
    The Pickaxe book has some good examples.

    -r.
    --
    Posted via http://www.ruby-forum.com/.
    Rodrigo Bermejo, Mar 14, 2008
    #3
  4. Guest

    Re: How to separate stdout & stderr from executed shell comm

    On Mar 14, 12:16 pm, Rodrigo Bermejo <>
    wrote:
    > unknown wrote:
    > > On Mar 14, 11:40 am, wrote:

    >
    > >> irb> my_exec_command("ls NON_existant_file")
    > >> => ["", "ls: NON_existant_file: No such file or directory\n", 2]

    >
    > >> Any pointers on how to implement my_exec_command?

    >
    > > PS: Unless there's a simpler way, if someone could just point me to an
    > > API that provides stdout and stderr of sub-processes as IO objects, I
    > > could probably figure it out from there. Thanks!

    >
    > Take a look at the IO class.
    > The Pickaxe book has some good examples.
    >
    > -r.
    > --
    > Posted viahttp://www.ruby-forum.com/.


    The closest API I could find is IO::popen. But as the doc for
    IO::popen says "...the subprocess's standard input and output will be
    connected..." So far I haven't found any way to get stdout and stderr
    of a sub-process *separately*.

    Any other pointers?
    , Mar 14, 2008
    #4
  5. James Gray Guest

    Re: How to separate stdout & stderr from executed shell comm

    On Mar 14, 2008, at 2:49 PM, wrote:

    > On Mar 14, 12:16 pm, Rodrigo Bermejo <>
    > wrote:
    >> unknown wrote:
    >>> On Mar 14, 11:40 am, wrote:

    >>
    >>>> irb> my_exec_command("ls NON_existant_file")
    >>>> => ["", "ls: NON_existant_file: No such file or directory\n", 2]

    >>
    >>>> Any pointers on how to implement my_exec_command?

    >>
    >>> PS: Unless there's a simpler way, if someone could just point me
    >>> to an
    >>> API that provides stdout and stderr of sub-processes as IO
    >>> objects, I
    >>> could probably figure it out from there. Thanks!

    >>
    >> Take a look at the IO class.
    >> The Pickaxe book has some good examples.
    >>
    >> -r.
    >> --
    >> Posted viahttp://www.ruby-forum.com/.

    >
    > The closest API I could find is IO::popen. But as the doc for
    > IO::popen says "...the subprocess's standard input and output will be
    > connected..." So far I haven't found any way to get stdout and stderr
    > of a sub-process *separately*.
    >
    > Any other pointers?


    Yes, Ruby comes with a standard library for this, called open3. Here
    are the docs for the method you want:

    $ qri Open3#popen3
    ----------------------------------------------------------- Open3#popen3
    popen3(*cmd) {|| ...}
    ------------------------------------------------------------------------
    Open stdin, stdout, and stderr streams and start external
    executable. Non-block form:

    require 'open3'

    [stdin, stdout, stderr] = Open3.popen3(cmd)

    Block form:

    require 'open3'

    Open3.popen3(cmd) { |stdin, stdout, stderr| ... }

    The parameter cmd is passed directly to Kernel#exec.

    Hope that helps.

    James Edward Gray II
    James Gray, Mar 14, 2008
    #5
  6. Guest

    Re: How to separate stdout & stderr from executed shell comm

    On Mar 14, 12:58 pm, James Gray <> wrote:
    > On Mar 14, 2008, at 2:49 PM, wrote:
    >
    >
    >
    > > On Mar 14, 12:16 pm, Rodrigo Bermejo <>
    > > wrote:
    > >> unknown wrote:
    > >>> On Mar 14, 11:40 am, wrote:

    >
    > >>>> irb> my_exec_command("ls NON_existant_file")
    > >>>> => ["", "ls: NON_existant_file: No such file or directory\n", 2]

    >
    > >>>> Any pointers on how to implement my_exec_command?

    >
    > >>> PS: Unless there's a simpler way, if someone could just point me
    > >>> to an
    > >>> API that provides stdout and stderr of sub-processes as IO
    > >>> objects, I
    > >>> could probably figure it out from there. Thanks!

    >
    > >> Take a look at the IO class.
    > >> The Pickaxe book has some good examples.

    >
    > >> -r.
    > >> --
    > >> Posted viahttp://www.ruby-forum.com/.

    >
    > > The closest API I could find is IO::popen. But as the doc for
    > > IO::popen says "...the subprocess's standard input and output will be
    > > connected..." So far I haven't found any way to get stdout and stderr
    > > of a sub-process *separately*.

    >
    > > Any other pointers?

    >
    > Yes, Ruby comes with a standard library for this, called open3. Here
    > are the docs for the method you want:
    >
    > $ qri Open3#popen3
    > ----------------------------------------------------------- Open3#popen3
    > popen3(*cmd) {|| ...}
    > ------------------------------------------------------------------------
    > Open stdin, stdout, and stderr streams and start external
    > executable. Non-block form:
    >
    > require 'open3'
    >
    > [stdin, stdout, stderr] = Open3.popen3(cmd)
    >
    > Block form:
    >
    > require 'open3'
    >
    > Open3.popen3(cmd) { |stdin, stdout, stderr| ... }
    >
    > The parameter cmd is passed directly to Kernel#exec.
    >
    > Hope that helps.
    >
    > James Edward Gray II


    Exactly what I was looking for. Thanks!
    , Mar 14, 2008
    #6
  7. Arlen Cuss Guest

    [Note: parts of this message were removed to make it a legal post.]

    Hi,

    require "open3"

    stdin, stdout, stderr = Open3.popen3('nroff -man')

    Try it!

    Arlen
    Arlen Cuss, Mar 14, 2008
    #7
    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. EvgueniB
    Replies:
    1
    Views:
    621
    Anthony Borla
    Dec 15, 2003
  2. Hans Deragon
    Replies:
    2
    Views:
    407
    Donn Cave
    Jun 4, 2004
  3. Une Bévue
    Replies:
    1
    Views:
    142
    Justin Collins
    Feb 27, 2008
  4. it_says_BALLS_on_your forehead
    Replies:
    2
    Views:
    261
    Joe Smith
    Jan 10, 2006
  5. Joe
    Replies:
    2
    Views:
    219
Loading...

Share This Page