How to use shell return value like $? In python?

Discussion in 'Python' started by aaabbb16@hotmail.com, Oct 24, 2011.

  1. Guest

    exp:
    os.system('ls -al')
    #I like to catch return value after this command. 0 or 1,2,3....
    does python support to get "$?"?
    then I can use something like:
    If $?==0:
    ........
    .................
    TIA
    david
     
    , Oct 24, 2011
    #1
    1. Advertising

  2. Guest

    On Oct 23, 7:44 pm, wrote:
    > exp:
    > os.system('ls -al')
    > #I like to catch return value after this command. 0 or 1,2,3....
    > does python support to get "$?"?
    > then I can use something like:
    >  If $?==0:
    >      ........
    > ................
    > TIA
    > david


    So for what I do is:
    r_number =os.system('ls -al')
    if r_number == 0
    .........
    .........
    any other way?
     
    , Oct 24, 2011
    #2
    1. Advertising

  3. Chris Rebert Guest

    On Sun, Oct 23, 2011 at 7:51 PM, <> wrote:
    > On Oct 23, 7:44 pm, wrote:
    >> exp:
    >> os.system('ls -al')
    >> #I like to catch return value after this command. 0 or 1,2,3....
    >> does python support to get "$?"?
    >> then I can use something like:
    >>  If $?==0:

    <snip>
    > So for what I do is:
    > r_number =os.system('ls -al')
    >     if r_number == 0
    >      .........
    >      .........
    > any other way?


    I would recommend using the `subprocess` module instead:
    http://docs.python.org/library/subprocess.html#convenience-functions

    Cheers,
    Chris
     
    Chris Rebert, Oct 24, 2011
    #3
  4. David Riley Guest

    On Oct 23, 2011, at 10:44 PM, wrote:

    > exp:
    > os.system('ls -al')
    > #I like to catch return value after this command. 0 or 1,2,3....
    > does python support to get "$?"?
    > then I can use something like:
    > If $?==0:
    > ........
    > ................


    From the manual (http://docs.python.org/library/os.html#os.system):

    "On Unix, the return value is the exit status of the process encoded in the format specified for wait(). Note that POSIX does not specify the meaning of the return value of the C system() function, so the return value of the Python function is system-dependent."

    From the linked wait() documentation, the data returned is in a 16-bit integer, with the high byte indicating the exit status (the low byte is the signal that killed the process). So:




    status = os.system("foo")

    retval, sig = ((status >> 8) & 0xFF), (status & 0xFF)




    In the above example, your return status will end up in "retval".

    Of course, you probably ought to be using subprocess to run your subprocesses anyway; it's a lot more powerful and a lot harder to enable things like shell injection attacks. See: http://docs.python.org/library/subprocess.html#subprocess-replacements (which, of course, shows a direct replacement for os.system which is just as vulnerable to shell injection)


    - Dave
     
    David Riley, Oct 24, 2011
    #4
  5. Nick Dokos Guest

    David Riley <> wrote:

    > On Oct 23, 2011, at 10:44 PM, wrote:
    >
    > > exp:
    > > os.system('ls -al')
    > > #I like to catch return value after this command. 0 or 1,2,3....
    > > does python support to get "$?"?
    > > then I can use something like:
    > > If $?==0:
    > > ........
    > > ................

    >
    > From the manual (http://docs.python.org/library/os.html#os.system):
    >
    > "On Unix, the return value is the exit status of the process encoded in the format specified for wait(). Note that POSIX does not specify the meaning of the return value of the C system() function, so the return value of the Python function is system-dependent."
    >
    > From the linked wait() documentation, the data returned is in a 16-bit integer, with the high byte indicating the exit status (the low byte is the signal that killed the process). So:
    >
    >
    >
    >
    > status = os.system("foo")
    >
    > retval, sig = ((status >> 8) & 0xFF), (status & 0xFF)
    >


    .... or

    retval, sig = os.WEXITSTATUS(status), os.WTERMSIG(status)

    for some insulation from low-level details.

    Nick
    >
    >
    >
    > In the above example, your return status will end up in "retval".
    >
    > Of course, you probably ought to be using subprocess to run your subprocesses anyway; it's a lot more powerful and a lot harder to enable things like shell injection attacks. See: http://docs.python.org/library/subprocess.html#subprocess-replacements (which, of course, shows a direct replacement for os.system which is just as vulnerable to shell injection)
    >
    >
    > - Dave
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
     
    Nick Dokos, Oct 24, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Greenhorn
    Replies:
    15
    Views:
    849
    Keith Thompson
    Mar 6, 2005
  2. Christian Heimes
    Replies:
    0
    Views:
    611
    Christian Heimes
    Feb 27, 2008
  3. Gerardo Herzig
    Replies:
    1
    Views:
    1,106
    Philipp Pagel
    Feb 27, 2008
  4. D'Arcy J.M. Cain
    Replies:
    0
    Views:
    879
    D'Arcy J.M. Cain
    Feb 27, 2008
  5. edwithad

    Bash shell to Python shell?

    edwithad, Sep 28, 2009, in forum: Python
    Replies:
    4
    Views:
    1,768
Loading...

Share This Page