[NUBY] Reading stderr, stdout and the exit status of a process

Problem: call an external process, wait till it finishes, get its stdout
and stderr as Strings, as well as the exit status.

My current approximation of the solution:

stdout = IO.popen("myprocess.sh").readlines
exitStatus = $?

It doesn't work. exitStatus comes out as nil. I probably understand why.

Naturally, one can always bite the bullet and write the whole story
(open a Process, attach two reader threads, direct all output streams
into Ruby strings, wait till the Process quits, etc). This would be a
lot of code for a common scripting task, and from past experiences doing
same thing with C and Java, there are a few interesting surprises.

Can some enlightened soul please show me The Ruby Way to do it all in
one line? Reference to an appropriate page in the Pickaxe that I should
have RTFM'd not to ask this silly question would be even better :slight_smile:

Domo arigato in advance.

Best regards,
Alexey Verkhovsky

see http://raa.ruby-lang.org/project/session/

example:

   jib:~ > cat a.rb
   require 'session'

   sh = Session.new

···

On Fri, 23 Jul 2004, Alexey Verkhovsky wrote:

Problem: call an external process, wait till it finishes, get its stdout
and stderr as Strings, as well as the exit status.

My current approximation of the solution:

stdout = IO.popen("myprocess.sh").readlines
exitStatus = $?

It doesn't work. exitStatus comes out as nil. I probably understand why.

Naturally, one can always bite the bullet and write the whole story
(open a Process, attach two reader threads, direct all output streams
into Ruby strings, wait till the Process quits, etc). This would be a
lot of code for a common scripting task, and from past experiences doing
same thing with C and Java, there are a few interesting surprises.

Can some enlightened soul please show me The Ruby Way to do it all in
one line? Reference to an appropriate page in the Pickaxe that I should
have RTFM'd not to ask this silly question would be even better :slight_smile:

Domo arigato in advance.

Best regards,
Alexey Verkhovsky

   #
   # example 0
   #
   puts '---'
   stdout, stderr = sh.execute 'ls no-exist'
   puts "stdout : #{ stdout }"
   puts "stderr : #{ stderr }"
   puts "exitstatus : #{ sh.exitstatus }"

   #
   # example 1
   #
   puts '---'
   sh.execute("ls #{ __FILE__ }") do |stdout,stderr|
     puts "stdout : #{ stdout }" if stdout
     puts "stderr : #{ stderr }" if stderr
   end
   puts "exitstatus : #{ sh.exitstatus }"

   #
   # example 2
   #
   puts '---'
   sh.outproc = proc{|stdout| puts "stdout : #{ stdout }"}
   sh.errproc = proc{|stderr| puts "stderr : #{ stderr }"}
   sh.execute 'echo $$'
   puts "exitstatus : #{ sh.exitstatus }"

   jib:~ > ruby a.rb
   ---
   stdout :
   stderr : ls: no-exist: No such file or directory
   exitstatus : 1
   ---
   stdout : a.rb
   exitstatus : 0
   ---
   stdout : 16452
   exitstatus : 0

you can do almost all of this using open3, but not exitstatus. it also
requires you to spawn a new process for every command if you want to
distinguish output.

regards.

-a
--

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
A flower falls, even though we love it;
and a weed grows, even though we do not love it. --Dogen

===============================================================================

Thank you very much!

Will you expect this module to work on Win32 platforms, or is it Unix
only?

Alex

···

On Thu, 2004-07-22 at 23:02, Ara.T.Howard wrote:

see http://raa.ruby-lang.org/project/session/

oh... ;-(

definitely *nix only - as it uses Open3 which, itself, relies on fork. i
probably could make it work on windows since i think there is a windows for
available now correct?

i don't know if what you want to do is possible on windows - any windows
experts out there?

-a

···

On Fri, 23 Jul 2004, Alexey Verkhovsky wrote:

On Thu, 2004-07-22 at 23:02, Ara.T.Howard wrote:

see http://raa.ruby-lang.org/project/session/

Thank you very much!

Will you expect this module to work on Win32 platforms, or is it Unix
only?

Alex

--

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
A flower falls, even though we love it;
and a weed grows, even though we do not love it. --Dogen

===============================================================================

i don't know if what you want to do is possible on windows - any windows
experts out there?

As far as I remember, NT and 2000 support POSIX calls, but XP doesn't
(although there is a library that you can download, or you can have
Cygwin, etc - not viable options for what I need to do). Looks like I'll
have to use the file system, OS-360 style.

Best regards,
Alex

google for 'create process' and window - i do think there is someway to do
this, i just don't know it...

cheers.

-a

···

On Fri, 23 Jul 2004, Alexey Verkhovsky wrote:

i don't know if what you want to do is possible on windows - any windows
experts out there?

As far as I remember, NT and 2000 support POSIX calls, but XP doesn't
(although there is a library that you can download, or you can have
Cygwin, etc - not viable options for what I need to do). Looks like I'll
have to use the file system, OS-360 style.

Best regards,
Alex

--

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
A flower falls, even though we love it;
and a weed grows, even though we do not love it. --Dogen

===============================================================================