#!/usr/bin/env bash
set -x
function createContent() {
python - <<END
import sys
if(true):
print('true')
else:
sys.exit(1)
END
}
if [ "${CREATE_REQUEST}" != "" ]; then
createContent
fi
#next operations
This is a shell script. There is python method defined in there called createContent . That method is called when CREATE_REQUEST Env variable is set. The problem is that when createContent runs sys.exit(1) the python function breaks but the shell script does not exit there. All other remaining code in the shell script continues to execute. Why is that happening and how to ensure that if python exits with sys.exit(1) the calling code either handles it or the shell just returns the control with correct exit code.
createContentis not a Python method. It's a shell function.set -e, notset -x. Then the shell script will stop after python sayssys.exit(1)createContent || exit 1set -emeans "exit as soon as there's a problem" (i.e. a non-zero return code from a pipeline). It can be countermanded withset +e.set -xmeans "echo what you're doing as you do it" and can be countermanded withset +x. For the complete set of bash shell options: gnu.org/software/bash/manual/html_node/The-Set-Builtin.html