From: Gennady Date: 2003-11-25T10:01:09+09:00 Subject: Re: Exit status on cmd executed via popen() 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/launch.rb 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-talk/72318 Gennady.