Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to use shell return value like $? In python?

Reply
Thread Tools

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

 
 
aaabbb16@hotmail.com
Guest
Posts: n/a
 
      10-24-2011
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


 
Reply With Quote
 
 
 
 
aaabbb16@hotmail.com
Guest
Posts: n/a
 
      10-24-2011
On Oct 23, 7:44*pm, (E-Mail Removed) 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?
 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      10-24-2011
On Sun, Oct 23, 2011 at 7:51 PM, <(E-Mail Removed)> wrote:
> On Oct 23, 7:44*pm, (E-Mail Removed) 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/subpr...ence-functions

Cheers,
Chris
 
Reply With Quote
 
David Riley
Guest
Posts: n/a
 
      10-24-2011
On Oct 23, 2011, at 10:44 PM, http://www.velocityreviews.com/forums/(E-Mail Removed) 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 >> & 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/subpr...s-replacements (which, of course, shows a direct replacement for os.system which is just as vulnerable to shell injection)


- Dave

 
Reply With Quote
 
Nick Dokos
Guest
Posts: n/a
 
      10-24-2011
David Riley <(E-Mail Removed)> wrote:

> On Oct 23, 2011, at 10:44 PM, (E-Mail Removed) 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 >> & 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/subpr...s-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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
return a value to shell script devi thapa Python 4 11-14-2008 09:01 PM
Get shell command return value in Perl program. Dan Perl Misc 2 08-15-2007 03:05 AM
No backtick shell return value Derek Basch Perl Misc 2 01-23-2007 01:13 AM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM



Advertisments