Bash Set Exit 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 about Stack Overflow the company Business Learn more bash fail on error about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges
Bash Exit On Error With Message
Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each bash exit on error code other. Join them; it only takes a minute: Sign up Stop on first error [duplicate] up vote 115 down vote favorite 28 Possible Duplicate: Automatic exit from bash shell script on error How can I have bash stop on bash script exit on error the first command failure, without putting stuff like this all through my code? some_prog || exit 1 some_other_prog || exit 1 bash share|improve this question asked Aug 13 '10 at 6:45 Matt Joiner 41.2k43213382 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
Bash Set Exit Code To Variable
comment| 1 Answer 1 active oldest votes up vote 237 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 48.8k1290125 31 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 69579 times active 6 years ago Linked 276 Automatic exit from bash shell script on error 192 What does set -e mean in a bash script? 62 How to have GNU make explicitly test for failure? 12 Execute multiple commands in a bash script sequentially and fail if at least one of them fails 1 Bash play sound on first error 0 Stop bash script when find gets to a folder with permission denied Related 487How to iterate over arguments in bash script1217In the shell, what does “ 2>&1 ” mean?1008How do I split a string on a delimiter in Bash?234How to pass all arguments passed to my bash script to a function of mine?577How to set a variable equal to the output from a c
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 bash set exit status result in scripts failing in unusual ways. It's possible to write scripts which bash throw error minimise these problems. In this article, I explain several techniques for writing robust bash scripts. Use set -u How often have
Bash Set -e
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 http://stackoverflow.com/questions/3474526/stop-on-first-error 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= david% bash -u /tmp/shrink-chroot.sh /tmp/shrink-chroot.sh: line 3: $1: unbound variable david% http://www.davidpashley.com/articles/writing-robust-shell-scripts/ 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 error checking, but I recommend you use this sparingly. set +e command1 command2 set -e On a slightly related note, by default
Ramey
The exit command terminates a script, just as in a C program. It can also return a value, which is available to the script's parent http://tldp.org/LDP/abs/html/exit-status.html process.Every command returns an exit status (sometimes referred to as a return status or exit code). A successful command returns a 0, while an unsuccessful one returns a non-zero value that usually can be interpreted as an error code. Well-behaved UNIX commands, programs, and utilities return a 0 exit code upon successful completion, though there are some exceptions.Likewise, functions within a script and the script itself return an exit status. The last command executed in the function or script determines the exit status. Within a script, an exit nnn command may be used to deliver an nnn exit status to the shell (nnn must be an integer in the 0 - 255 range).When a script ends with an exit that has no parameter, the exit status of the script is the exit status of the last command executed in the script (previous to the exit).#!/bin/bash COMMAND_1 . . . COMMAND_LAST # Will exit with status of last command. exitThe equivalent of a bare exit is exit $? or even just omitting the exit.#!/bin/bash COMMAND_1 . . . COMMAND_LAST # Will exit with status of last command. exit $?#!/bin/bash COMMAND1 . . . COMMAND_LAST # Will exit with status of last command.