0
#!/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.

11
  • Note: createContent is not a Python method. It's a shell function. Commented Feb 2, 2022 at 23:11
  • 3
    You need set -e, not set -x. Then the shell script will stop after python says sys.exit(1) Commented Feb 2, 2022 at 23:13
  • 4
    createContent || exit 1 Commented Feb 2, 2022 at 23:13
  • 3
    unrelated, but your python code isn't syntatically valid Commented Feb 2, 2022 at 23:16
  • 1
    @colossal They're two different shell options, independent of each other. set -e means "exit as soon as there's a problem" (i.e. a non-zero return code from a pipeline). It can be countermanded with set +e. set -x means "echo what you're doing as you do it" and can be countermanded with set +x. For the complete set of bash shell options: gnu.org/software/bash/manual/html_node/The-Set-Builtin.html Commented Feb 2, 2022 at 23:47

1 Answer 1

2

By default, shell scripts do not exit if a command returns non zero (fails). You can set this behaviour with set -e.

If you don't want set -e for the whole script, you can add it to the function only:

createContent () {
python - <<"END" || exit 1
# python code
"END"
}

You could also exit only for a specific error code:

python - <<"END"; (($?==1)) && exit 1

You should double quote END, to ensure the script text is passed verbatim (without quotes the text is subject to shell expansion).

If you don't want it built in to the function by default, you can add the same logic when calling the function:

createContent || exit 1
Sign up to request clarification or add additional context in comments.

3 Comments

This works good but one thing I am noticing is that python's sys.exit(1) seems to be different than shell's exit 1. The k8 pod shows its status as Error where this script runs in a container where as in cases where shell returns exit 1 for some other use case shows Completed. Is sys.exit(1) different from exit 1 ?
@colossal: Both forms end the current process (closing open file handles and other cleanup tasks first) and return status code 1 to the calling process. If you see a different behaviour, ask a new question about this, and don't forget how you call the respective process from the container.
@colossal They are essentially equivalent commands yes. The different behaviour might be something to do with the Python script being a child of the shell script. I recommend asking a new question, tagged kubernetes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.