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

A

aaabbb16

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
 
A

aaabbb16

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?
 
D

David Riley

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
 
N

Nick Dokos

David Riley said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top