Handling python exit code in shell script

I am calling a python script from a shell script. Python script returns error codes in case of failures.

How to handle these error codes in a shell script and exit it when necessary?

+6
source share
2 answers

The completion code for the last command is in $? .

Use below pseudo code:

 python myPythonScript.py ret=$? if [ $ret -ne 0 ]; then #Handle failure #exit if required fi 
+9
source

Do you mean the variable $? ?

 $ python -c 'import foobar' > /dev/null Traceback (most recent call last): File "<string>", line 1, in <module> ImportError: No module named foobar $ echo $? 1 $ python -c 'import this' > /dev/null $ echo $? 0 
+2
source

All Articles