Every system I have used in the past continues my simple bash scripts if one line returns a non-zero result. Some new Ubuntu LTS 14.x systems now exit on the first failure. I used
echo $-
and e does not appear in the list. What else should I be looking for?
Added from comment:
$ declare -f command_not_found_handle
command_not_found_handle ()
{
if [ -x /usr/lib/command-not-found ]; then
/usr/lib/command-not-found -- "$1";
return $?;
else
if [ -x /usr/share/command-not-found/command-not-found ]; then
/usr/share/command-not-found/command-not-found -- "$1";
return $?;
else
printf "%s: command not found\n" "$1" 1>&2;
return 127;
fi;
fi
}
Use a bash trap in your script, see the example below of a bash script:
#!/usr/bin/bash
main() {
trap 'error_handler ${LINENO} $?' ERR
###### put your commands in the following
echo "START"
non_existent_command
echo "END"
}
error_handler() {
process="$0"
lastline="$1"
lasterr="$2"
printf 'ERROR: %s: line %s: last command exit status: %s \n' "$process" "$lastline" "$lasterr"
trap - ERR
exit 0
}
main
if you try to launch a non existent command (non_existent_command
in the example) or a command with exit status different from 0 the trap will activate the error_handler
function which contains the exit command exit 0
.
In the sample above the output will be:
>START
>./new.sh: line 8: non_existent_command: command not found
>ERROR: ./new.sh: line 8: last command exit status: 127
Note that "START" is printed but "END" not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With