Halt Bash 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 bash exit on error with message workings and policies of this site About Us Learn more about shell script error handling Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions bash disable exit on error Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Bash Stop Script
Join them; it only takes a minute: Sign up Stop on first error [duplicate] up vote 121 down vote favorite 28 Possible Duplicate: Automatic exit from bash shell script on error How can I have bash stop on the first command failure, without putting stuff like this all through my code? some_prog || exit 1 some_other_prog || exit 1 bash errexit bash share|improve this question asked Aug 13 '10 at 6:45 Matt Joiner 41.6k44216383 marked as duplicate by martin clayton, Barmar, Toto, codesparkle, Emil Vikström Oct 13 '12 at 10:31 This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question. add a comment| 1 Answer 1 active oldest votes up vote 241 down vote accepted Maybe you want set -e: http://www.davidpashley.com/articles/writing-robust-shell-scripts.html#id2382181 share|improve this answer answered Aug 13 '10 at 6:50 Alok Singhal 49.2k1291126 32 Be aware of set -e gotchas: mywiki.wooledge.org/BashFAQ/105 –Kris Jun 9 '15 at 10:57 1 @Kris thanks for the link, very interesting. –Alok Singhal Jun 10 '15 at 3:45 2 @Kris, you just broke my heart. I thought -e was infallible. –Trenton Sep 30 '15 at 3:17 add a comment| Not the answer you're looking for? Browse other questions tagged bash or ask your own question. asked 6 years ago viewed 70786 times active 6 years ago Linked 282 Automatic exit from bash she
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 bash function exit on error site About Us Learn more about Stack Overflow the company Business Learn more
Bash Set +e
about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss
Bash Print Commands
Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up In a bash http://stackoverflow.com/questions/3474526/stop-on-first-error script, how can I exit the entire script if a certain condition occurs? up vote 238 down vote favorite 51 I'm writing a script in Bash to test some code. However, it seems silly to run the tests if compiling the code fails in the first place, in which case I'll just abort the tests. Is there a way I can do this without http://stackoverflow.com/questions/1378274/in-a-bash-script-how-can-i-exit-the-entire-script-if-a-certain-condition-occurs wrapping the entire script inside of a while loop and using breaks? Something like a dun dun dun goto? bash scripting share|improve this question asked Sep 4 '09 at 9:51 samoz 20.8k39109168 add a comment| 6 Answers 6 active oldest votes up vote 226 down vote accepted Try this statement: exit 1 Replace 1 with appropriate error codes. See also Exit Codes With Special Meanings. share|improve this answer edited Aug 7 '15 at 7:10 flying sheep 2,96722245 answered Sep 4 '09 at 9:53 Michael Foukarakis 20.6k35090 42 0 isn't good status to exit with if an error happens. –Michał Górny Sep 4 '09 at 9:59 4 You're right, edited for clarity. –Michael Foukarakis Sep 4 '09 at 10:00 @MichałGórny what would be a good status code? –CMCDragonkai May 14 '14 at 2:39 3 @CMCDragonkai, usually any non-zero code will work. If you don't need anything special, you can just use 1 consistently. If the script is meant to be run by another script, you may want to define your own set of status code with particular meaning. For example, 1 == tests failed, 2 == compilation fa
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 of subtle effects which result in scripts failing in http://www.davidpashley.com/articles/writing-robust-shell-scripts/ unusual ways. It's possible to write scripts which minimise these problems. In this article, http://unix.stackexchange.com/questions/82224/how-to-make-bash-abort-the-execution-of-a-script-on-syntax-error 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 script above and accidentally forgot to give a parameter, you would have just deleted all of your on error 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= 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 exit on error 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 error checking, but I recommend you use this sparingly. set +e command1 command2 set -e On a slightly related note, by default bash takes the error status of the last item in a pipeline, which may not be what you want. For example, false | true will be considered to have succeeded. I
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 someth