Reputation: 175
I am trying to write a shell script that essentially calls a bunch of functions in another script. The script looks like:
#!/bin/bash
set -e
function cleanup() {
exitStatus=$?
if [ $exitStatus -ne 0 ]; then
rm -rf /tmp/alpha*
fi
exit $exitStatus
}
trap cleanup ERR
VAR1="test"
. /home/common/scripts/utils.sh
echo "Executing function1"
function1 $VAR1
echo "Executing function2"
function2 $VAR1
exit $?
My question is that do I need to set -e as well as trap in order to make sure that my script is robust here or just using trap is enough.
Second: If I set -e in this script then do I need to set -e in utils.sh
too in order to make sure that the function is executing correctly in that script or the set -e in this script will take care of that?
Upvotes: 1
Views: 564
Reputation: 40749
set -e
just exits the script immediately if there is an error, instead of continuing. Whether or not to use it depends on the behaviour you want.
The trap
just handles the error on exit, so if you're concerned about robustness the combination of the two is probably good.
You should put set -e
in each sub-script if you want them to terminate immediately on error too.
Upvotes: 2