I have put
set -e
set -u
at the top of my BASH script to make it fail as opposed to going on.
Is there a way for me to specify some code to be run on failure?
If there’s no better way, I can make my program a three files:
a
.a
.a_onfail
and have a just be
# headers etc
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
$(DIR)/.a || $(DIR)/.a_onfail
You could try solve this using trap. Eg:
trap "ERROR=1" ERRandtrap '[ "$ERROR" = "1" ] && on_error' EXITand then provide aon_errorfunction.