How To Stop A Shell Script On Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more shell script error handling about Stack Overflow the company Business Learn more about hiring developers or posting
Bash Exit On Error With Message
ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack shell script exit code Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Exit a Script On Error up vote 43 down vote favorite bash fail command 7 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? bash exit shell share|improve this question edited Nov 9
Bash If Exit Code
'15 at 14:39 NargothBond 4922722 asked Dec 7 '10 at 21:10 Nathan Campos 10.3k38149260 add a comment| 5 Answers 5 active oldest votes up vote 33 down vote accepted Are you looking for exit? This is the best bash guide around. http://tldp.org/LDP/abs/html/ In 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 ... share|improve this answer edited Apr 23 '14 at 9:13 MattBianco 815921 answered Dec 7 '10 at 21:13 Byron Whitlock 35k1985141 2 If you like the ABS, you'll love the BashGuide, BashFAQ and BashPitfalls. –Dennis Williamson Dec 8 '10 at 4:45 add a comment| up vote 171 down vote 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 condi
and Signals and Traps (Oh My!) - Part 1 by William Shotts, Jr. In this lesson, we're going to look at handling errors during the execution of your scripts. The difference between a good program and a poor one is often measured in terms of the program's robustness. That bash catch error is, the program's ability to handle situations in which something goes wrong. Exit status As you bash function exit on error recall from previous lessons, every well-written program returns an exit status when it finishes. If a program finishes successfully, the exit status will be zero.
Unix Exit Codes
If the exit status is anything other than zero, then the program failed in some way. It is very important to check the exit status of programs you call in your scripts. It is also important that your scripts return http://stackoverflow.com/questions/4381618/exit-a-script-on-error a meaningful exit status when they finish. I once had a Unix system administrator who wrote a script for a production system containing the following 2 lines of code: # Example of a really bad idea cd $some_directory rm * Why is this such a bad way of doing it? It's not, if nothing goes wrong. The two lines change the working directory to the name contained in $some_directory and delete the files in that directory. That's the intended behavior. But what http://linuxcommand.org/wss0150.php happens if the directory named in $some_directory doesn't exist? In that case, the cd command will fail and the script executes the rm command on the current working directory. Not the intended behavior! By the way, my hapless system administrator's script suffered this very failure and it destroyed a large portion of an important production system. Don't let this happen to you! The problem with the script was that it did not check the exit status of the cd command before proceeding with the rm command. Checking the exit status There are several ways you can get and respond to the exit status of a program. First, you can examine the contents of the $? environment variable. $? will contain the exit status of the last command executed. You can see this work with the following: [me] $ true; echo $? 0 [me] $ false; echo $? 1 The true and false commands are programs that do nothing except return an exit status of zero and one, respectively. Using them, we can see how the $? environment variable contains the exit status of the previous program. So to check the exit status, we could write the script this way: # Check the exit status cd $some_directory if [ "$?" = "0" ]; then rm * else echo "Cannot change directory!" 1>&2 exit 1 fi In this version, we examine the exit status of the cd command and if it's not zero, w
Bash Prompts About Writing Robust Bash Shell Scripts Many people hack together shell scripts quickly to do simple tasks, but these soon take on a life of their own. Unfortunately shell scripts are full http://www.davidpashley.com/articles/writing-robust-shell-scripts/ of subtle effects which result in scripts failing in unusual ways. It's possible http://unix.stackexchange.com/questions/82224/how-to-make-bash-abort-the-execution-of-a-script-on-syntax-error to write scripts which minimise these problems. In this article, I explain several techniques for writing robust bash scripts. Use set -u How often have you written a script that broke because a variable wasn't set? I know I have, many times. chroot=$1 ... rm -rf $chroot/usr/share/doc If you ran the shell script script above and accidentally forgot to give a parameter, you would have just deleted all of your system documentation rather than making a smaller chroot. So what can you do about it? Fortunately bash provides you with set -u, which will exit your script if you try to use an uninitialised variable. You can also use the slightly more readable set -o nounset. david% bash /tmp/shrink-chroot.sh $chroot= exit on error david% bash -u /tmp/shrink-chroot.sh /tmp/shrink-chroot.sh: line 3: $1: unbound variable david% Use set -e Every script you write should include set -e at the top. This tells bash that it should exit the script if any statement returns a non-true return value. The benefit of using -e is that it prevents errors snowballing into serious issues when they could have been caught earlier. Again, for readability you may want to use set -o errexit. Using -e gives you error checking for free. If you forget to check something, bash will do it or you. Unfortunately it means you can't check $? as bash will never get to the checking code if it isn't zero. There are other constructs you could use: command if [ "$?"-ne 0]; then echo "command failed"; exit 1; fi could be replaced with command || { echo "command failed"; exit 1; } or if ! command; then echo "command failed"; exit 1; fi What if you have a command that returns non-zero or you are not interested in its return value? You can use command || true, or if you have a longer section of code, you can turn off the
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Unix & Linux Questions Tags Users Badges Unanswered Ask Question _ Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top How to make bash abort the execution of a script on syntax error? up vote 14 down vote favorite 5 To be on safe side, I'd like bash abort the execution of a script if it encounters a syntax error. To my surprise, I can't achieve this. (set -e is not enough.) Example: #!/bin/bash # Do exit on any error: set -e readonly a=(1 2) # A syntax error is here: if (( "${a[#]}" == 2 )); then echo ok else echo not ok fi echo status $? echo 'Bad: has not aborted execution on syntax error!' Result (bash-3.2.39 or bash-3.2.51): $ ./sh-on-syntax-err ./sh-on-syntax-err: line 10: #: syntax error: operand expected (error token is "#") status 1 Bad: has not aborted execution on syntax error! $ Well, we can't check $? after every statement to catch syntax errors. (I expected such safe behavior from a sensible programming language... perhaps this must be reported as a bug/wish to bash developers) More experiments if makes no difference. Removing if: #!/bin/bash set -e # exit on any error readonly a=(1 2) # A syntax error is here: (( "${a[#]}" == 2 )) echo status $? echo 'Bad: has not aborted execution on syntax error!' Result: $ ./sh-on-syntax-err ./sh-on-syntax-err: line 6: #: syntax error: operand expected (error token is "#") status 1 Bad: has not aborted execution on syntax error! $ Perhaps, it's related to exercise 2 from http://mywiki.wooledge.org/BashFAQ/105 and has something to do with (( )). But I find it still unreasonable to continue executing afte a syntax error. No, (( )) makes no difference! It be