Professional Web Applications Themes

Exit status on cmd executed via popen() - Ruby

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 ...

  1. #1

    Default Exit status on cmd executed via popen()

    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 = netel.rpi.edu
    Senior Systems Programmer or org
    Rensselaer Polytechnic Institute or edu


    Garance Guest

  2. #2

    Default Re: Exit status on cmd executed via popen()

    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: 



    Gennady Guest

  3. #3

    Default Re: Exit status on cmd executed via popen()

    At 7:27 AM +0900 11/25/03, Gennady wrote: 

    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 = netel.rpi.edu
    Senior Systems Programmer or org
    Rensselaer Polytechnic Institute or edu


    Garance Guest

  4. #4

    Default Re: Exit status on cmd executed via popen()



    Garance A Drosihn wrote: 
    >
    >
    > 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!
    >[/ref]

    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.



    Gennady Guest

Similar Threads

  1. Net::telnet - need to retrieve the exit status
    By Anita in forum PERL Modules
    Replies: 1
    Last Post: September 2nd, 05:13 AM
  2. ksh script, exit status and pipe
    By Ian Wilson in forum Linux / Unix Administration
    Replies: 1
    Last Post: August 25th, 12:49 PM
  3. Script Exit Status
    By Paul Kraus in forum Perl / CGI
    Replies: 2
    Last Post: December 2nd, 05:36 PM
  4. DBI: exit status, sql operation
    By Christian Eriksson in forum PERL Modules
    Replies: 1
    Last Post: November 29th, 06:15 PM
  5. Replies: 0
    Last Post: July 25th, 10:21 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139