Greetings,

On Fri, 5 Apr 2002, Wladimir Mutel wrote:
>
> 	I think IO.popen already does something similar.

I have a related question to that:

Short version:

Is there a way to get the output of an external program and
the error code as well (and even better: the output written to
STDERR)?

Long version:

1. To run a simple command and get the output and the error code,
I could use the backticks:

irb(main):001:0> a=`date`; puts $?
0
nil
irb(main):002:0> a=`foo`; puts $?>>8
(irb):10: command not found: foo
127

2. Now let's assume, the command I want to run, takes a long time:
(long takes some time and return error code 0).
irb(main):003:0> a=`long`
"Sleeping\nWoke up\n"
irb(main):004:0> puts $?
0

This will return after the command finished, no way to get the
output while running (not satisfactory).

3. OK, next try: Use IO.popen

irb(main):005:0> f=IO.popen("long")
#<IO:0x40152588>
irb(main):006:0> while str=f.gets; puts str; end
Sleeping
Woke up

Also, I could wrap it up in a thread and do something else, while
waiting (almost there).

4. Remaining problem: How to get the error code with IO.popen
(err is program which returns with an error code != 0 after some time).

irb(main):007:0> a=`date`
irb(main):008:0> f=IO.popen("err")
#<IO:0x40152588>
irb(main):009:0> while str=f.gets; puts str; end; puts $?>>8
Sleeping
Woke up
0 (this is not the real error code)

5. Next problem: How do I capture program output written to STDERR?
(OK, I could redirect 2>&1, but that's no real solution)

Thanks for your hints.

Arno Erpenbeck