Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to separate stdout & stderr from executed shell command

Reply
Thread Tools

How to separate stdout & stderr from executed shell command

 
 
jqwoods@gmail.com
Guest
Posts: n/a
 
      03-14-2008
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?
 
Reply With Quote
 
 
 
 
jqwoods@gmail.com
Guest
Posts: n/a
 
      03-14-2008
On Mar 14, 11:40 am, (E-Mail Removed) 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!
 
Reply With Quote
 
 
 
 
Rodrigo Bermejo
Guest
Posts: n/a
 
      03-14-2008
unknown wrote:
> On Mar 14, 11:40 am, (E-Mail Removed) 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/.

 
Reply With Quote
 
jqwoods@gmail.com
Guest
Posts: n/a
 
      03-14-2008
On Mar 14, 12:16 pm, Rodrigo Bermejo <(E-Mail Removed)>
wrote:
> unknown wrote:
> > On Mar 14, 11:40 am, (E-Mail Removed) 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:open. But as the doc for
IO:open 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?
 
Reply With Quote
 
James Gray
Guest
Posts: n/a
 
      03-14-2008
On Mar 14, 2008, at 2:49 PM, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> On Mar 14, 12:16 pm, Rodrigo Bermejo <(E-Mail Removed)>
> wrote:
>> unknown wrote:
>>> On Mar 14, 11:40 am, (E-Mail Removed) 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:open. But as the doc for
> IO:open 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


 
Reply With Quote
 
jqwoods@gmail.com
Guest
Posts: n/a
 
      03-14-2008
On Mar 14, 12:58 pm, James Gray <(E-Mail Removed)> wrote:
> On Mar 14, 2008, at 2:49 PM, (E-Mail Removed) wrote:
>
>
>
> > On Mar 14, 12:16 pm, Rodrigo Bermejo <(E-Mail Removed)>
> > wrote:
> >> unknown wrote:
> >>> On Mar 14, 11:40 am, (E-Mail Removed) 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:open. But as the doc for
> > IO:open 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!
 
Reply With Quote
 
Arlen Cuss
Guest
Posts: n/a
 
      03-14-2008
[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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
IO.popen how to separate $stdout and $stderr Une Bévue Ruby 1 02-27-2008 12:52 AM
script hangs when run from command line and redirecting stdout and stderr to file it_says_BALLS_on_your forehead Perl Misc 2 01-10-2006 11:31 AM
Fetching the stdout & stderr as it flows from a unix command. Hans Deragon Python 2 06-04-2004 08:04 PM
How to use several separate classes (separate files) to be executed in one class (another file) EvgueniB Java 1 12-15-2003 01:18 AM



Advertisments