Running a Python script within shell script - Check status

Hate to resurrect a dinosaur but while this selected answer worked as written I wanted to add a variation where one can check against multiple exit codes. $? only seems to retrieve the exit code of the last executed program one time. So if you need to check the exit code against multiple cases, it is necessary to assign $? to a variable and then check the variable, a la (using @chepner 's example):

python script.py
exit_code=$?
if [[ $exit_code = 0 ]]; then
    echo "success"
elif [[ $exit_code = 1 ]]; then
    echo "a different form of success, maybe"
else
    echo "failure: $exit_code"
fi

First, you can pass the desired exit code as an argument to sys.exit in your python script.

Second, the exit code of the most recently exited process can be found in the bash parameter $?. However, you may not need to check it explicitly:

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

To check the exit code explicitly, you need to supply a conditional expression to the if statement:

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