Running Python script inside shell script - Check status

In my shell script, I ran this command:

python script.py

I was wondering how the question is in two parts:

  • How can I program my python script to pass the status back to a shell script that ran it depending on what happens in the python script. For example, if something goes wrong in a python script, let it exit with a code of 1, which is sent back to the shell.

  • How can I get my shell script to read python exit code and exit due to an error? For example, the status code is nothing but 0, then it will exit.

+5
source share
1 answer

-, sys.exit python script.

-, bash $?. :

if python script.py; then
    echo "Exit code of 0, success"
else
    echo "Exit code of $?, failure"
fi

, if:

python script.py
if [[ $? = 0 ]]; then
    echo "success"
else
    echo "failure: $?"
fi
+13

All Articles