Bash script exit after 1st nonzero result, although -e is not set to ENV

Every system I 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 come out of the first failure. I used

echo $- 

and e does not appear in the list. What else should I look 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 } 
+6
source share
1 answer

Use a bash trap in a script, see the example below 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 run a nonexistent command ( non_existent_command in the example) or a command with an exit status other than 0, the trap activates the error_handler function, which contains the exit exit 0 . In the above example, the output would be:

 >START >./new.sh: line 8: non_existent_command: command not found >ERROR: ./new.sh: line 8: last command exit status: 127 

Please note that β€œSTART” is printed, but β€œEND” is not.

+1
source

Source: https://habr.com/ru/post/979012/


All Articles