Exit Script On Error

I am creating a Script wrapper that has an if function similar to this:

 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 the error message is displayed. How can i do this?

+72
bash shell exit
Dec 07 '10 at 21:10
source share
5 answers

Are you looking for exit ?

This is the best bash guide. http://tldp.org/LDP/abs/html/

In the context:

 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 1>&2 exit 1 # terminate and indicate error fi ... 
+65
Dec 07 '10 at 21:13
source share

If you put set -e in a script, the script exits as soon as any command inside it fails (i.e. as soon as any command returns a non-zero status). This does not allow you to write your own message, but often enough messages about an unsuccessful team.

The advantage of this approach is that it is automatic: you do not risk forgetting about the error with the error.

Commands whose condition is checked conditionally (for example, if , && or || ) do not complete the script (otherwise conditional would be pointless). An idiom for a random team whose failure does not matter, command-that-may-fail || true command-that-may-fail || true You can also disable set -e for the script part with set +e .

+244
Dec 07 2018-10-12T00:
source share

If you want to be able to handle the error instead of blind exit, use trap in the ERR pseudo signal instead of using set -e .

 #!/bin/bash f () { errcode=$? # save the exit code as the first thing done in the trap function echo "error $errorcode" echo "the command executing at the time of the error was" echo "$BASH_COMMAND" echo "on line ${BASH_LINENO[0]}" # do some error handling, cleanup, logging, notification # $BASH_COMMAND contains the command that was being executed at the time of the trap # ${BASH_LINENO[0]} contains the line number in the script of that command # exit the script or return to try again, etc. exit $errcode # or use some other value or do return instead } trap f ERR # do some stuff false # returns 1 so it triggers the trap # maybe do some other stuff 

Other traps can be configured to process other signals, including regular Unix signals, as well as other pseudo Bash RETURN and DEBUG signals.

+31
Dec 08 '10 at 5:12
source share

Here's how to do it:

 #!/bin/sh abort() { echo >&2 ' *************** *** ABORTED *** *************** ' echo "An error occurred. Exiting..." >&2 exit 1 } trap 'abort' 0 set -e # Add your script below.... # If an error occurs, the abort() function will be called. #---------------------------------------------------------- # ===> Your script goes here # Done! trap : 0 echo >&2 ' ************ *** DONE *** ************ ' 
+4
Mar 05 '14 at 15:43
source share

exit 1 is all you need. 1 is a return code, so you can change it if you want, for example, 1 means a successful run, and -1 means a failure or something like that.

-5
Dec 07 '10 at 21:14
source share



All Articles