I have added the following function to my bash example script:
# Run external command and only procced, when exit code is 0
function try {
$@
returnValue=$?
echo "$returnValue"
if [ $returnValue -ne 0 ]
then
log $ERROR "$@ exited with return value
$returnValue Script will stop here."
echo "$@ exited with return value $returnValue.
Script will stop here."
exit $EXIT_FAILURE
fi
}
The function takes a program as an argument and runs in. If the program returns with a status other then 0, so it fails, then the script will print an error message, log it and and exits. I needed this behaviour in some recent scripts and hope it to be usefull for everybody else.
1 thought on “Bash Example Script #2”
Comments are closed.