How to keep a bash script running when "set + e" is not enough?

I call a command written in C ++ from a bash script (on Ubuntu 10.10). The command throws an exception and exits, and the script aborts.

Even with "set + e" or "command || true", the script will not continue.

How can I get the script to continue?

+4
source share
1 answer

A shell script can capture any signal except 9 (KILL) using the "trap" command. The name of the special signal "ERR" means any non-zero error status.

trap 'error=1' ERR while true; do run-external-program code="$?" if [[ -n "$error" ]]; then echo "Caught an error! Status = $code" error= # reset the error elif [[ "$code" != 0 ]]; then echo "Program exited with non-zero code: $code" fi done 

You can also tell the shell to ignore errors with an empty command:

  trap '' ERR 
+3
source

All Articles