1

I have a bash script that run some tool that on some use cases returns an error code of 137. I use this script as part of my CI pipeline and and as far as i am concerned this is suceessful state. My question is how can i catch this exit code and return an exit code of 0 instead(like trap command, only the I do not think it support custom exit codes, only predefined one).

Thanks in advance, Alon

1 Answer 1

1

After you run the command/tool in your script:

command

You can see what the exit code and do some logic based on that return:

command
return=$?
if [ $return -eq 137 ]; then
    exit 0
else # @chepner's very good suggestion
    exit $return 
fi

That will end your bash script with an exit code of 0 if the command you ran (your tool) returns an exit code of 137.

Sign up to request clarification or add additional context in comments.

8 Comments

Be sure to have an explicit exit $return in an else clause. Otherwise, where the exit status of the script was implicitly set by the exit status of command before, now the exit status would be the exit status of if.
Don't use set -e if you want control over the exit status of your script.
Check out some of these answers for set -e: stackoverflow.com/questions/26675681/…
The problem is that i can't use it because i use "set -e" in my script(because it is in CI pipeline i want that each failure that is not 137 will fail the pipeline). trap command was great for handling it with predefined signals such as SIGINT or SIGTERM, this time i want to handle a custom exit code returned by some tool.
@AlonTsaraf put this answer's code into a new Bash script, add set +e at the top to temporarily disable the set -e in this script, and then call this wrapper script from your main script instead of the real command.
|

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.