I'm building a Shell Script that has a if
function like this one:
if jarsigner -verbose -keystore $keyst -keystore $pass $jar_file $kalias
then
echo $jar_file signed sucessfully
else
echo ERROR: Failed to sign $jar_file. Please recheck the variables
fi
...
I want the execution of the script to finish after displaying the error message. How I can do this?
Are you looking for
exit
?This is the best bash guide around. http://tldp.org/LDP/abs/html/
In context:
If you want to be able to handle an error instead of blindly exiting, instead of using
set -e
, use atrap
on theERR
pseudo signal.Other traps can be set to handle other signals, including the usual Unix signals plus the other Bash pseudo signals
RETURN
andDEBUG
.Here is the way to do it:
If you put
set -e
in a script, the script will terminate as soon as any command inside it fails (i.e. as soon as any command returns a nonzero status). This doesn't let you write your own message, but often the failing command's own messages are enough.The advantage of this approach is that it's automatic: you don't run the risk of forgetting to deal with an error case.
Commands whose status is tested by a conditional (such as
if
,&&
or||
) do not terminate the script (otherwise the conditional would be pointless). An idiom for the occasional command whose failure doesn't matter iscommand-that-may-fail || true
. You can also turnset -e
off for a part of the script withset +e
.exit 1
is all you need. The1
is a return code, so you can change it if you want, say,1
to mean a successful run and-1
to mean a failure or something like that.