[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Exit status on cmd executed via popen

Garance A Drosihn

11/24/2003 10:09:00 PM

Sometimes I write ruby scripts to filter the output of some
other command. So I write something like:

cmdout = IO.popen(somecmd, "r")
cmdout.each_line {|aline|
# Do Stuff...
}
cmdout.close

Is there any way to find out what the exit status was from
the command given to popen? It would be nice to have some
method that I could use like:

if cmdout.popen_exit_status != 0
# Do Error-processing stuff
end

which could be done after reading all the lines, but before
the call to 'close'.

Or do I need to do something more complicated than 'popen'
if I need to know the exit code from that command?

--
Garance Alistair Drosehn = gad@gilead.netel.rpi.edu
Senior Systems Programmer or gad@freebsd.org
Rensselaer Polytechnic Institute or drosih@rpi.edu


3 Answers

Gennady

11/24/2003 10:27:00 PM

0

Try variable $?, it holds the status of the last executed command. Works
for commands executed with 'system', I think it is valid for popen as well.

Gennady.

Garance A Drosihn wrote:
> Sometimes I write ruby scripts to filter the output of some
> other command. So I write something like:
>
> cmdout = IO.popen(somecmd, "r")
> cmdout.each_line {|aline|
> # Do Stuff...
> }
> cmdout.close
>
> Is there any way to find out what the exit status was from
> the command given to popen? It would be nice to have some
> method that I could use like:
>
> if cmdout.popen_exit_status != 0
> # Do Error-processing stuff
> end
>
> which could be done after reading all the lines, but before
> the call to 'close'.
>
> Or do I need to do something more complicated than 'popen'
> if I need to know the exit code from that command?
>



Garance A Drosihn

11/25/2003 12:23:00 AM

0

At 7:27 AM +0900 11/25/03, Gennady wrote:
>Try variable $?, it holds the status of the last executed
>command. Works for commands executed with 'system', I think
>it is valid for popen as well.

Interesting. In the code sequence of:

cmdout = IO.popen(somecmd, "r")
cmdout.each_line {|aline|
# Do Stuff...
}
cmdout.close

I had tried checking $? just before cmdout.close, and it had a
value of nil. However, checking it immediately *after* the
close, it does seem to have $? set to the desired value. As
long as I can depend on this, it will do the job very nicely.
Thanks!

--
Garance Alistair Drosehn = gad@gilead.netel.rpi.edu
Senior Systems Programmer or gad@freebsd.org
Rensselaer Polytechnic Institute or drosih@rpi.edu


Gennady

11/25/2003 1:01:00 AM

0



Garance A Drosihn wrote:
> At 7:27 AM +0900 11/25/03, Gennady wrote:
>
>> Try variable $?, it holds the status of the last executed
>> command. Works for commands executed with 'system', I think
>> it is valid for popen as well.
>
>
> Interesting. In the code sequence of:
>
> cmdout = IO.popen(somecmd, "r")
> cmdout.each_line {|aline|
> # Do Stuff...
> }
> cmdout.close
>
> I had tried checking $? just before cmdout.close, and it had a
> value of nil. However, checking it immediately *after* the
> close, it does seem to have $? set to the desired value. As
> long as I can depend on this, it will do the job very nicely.
> Thanks!
>

I have implemented a method available through Kernel.launch. It allows
you to execute several commands connecting all of them in a pipeline. I
makes available each command's return status together with stderr
messages. The last command's stdout is also available. I haven't
released it, however if you are interested you can get it at

http://homepage.mac.com/bystr/Development/Ruby...

It has a set of unit tests attached, so it should be easy to figure out
the usage. I am not sure if it works on Windows as it uses fork() and
Process.waitpid2()

I mentioned it on ruby-talk before, you may find some description there:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-...

Gennady.