Bash Scripting 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
Bash Script Exit With Error Message
site About Us Learn more about Stack Overflow the company Business Learn more bash shell script exit on error about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x bash exit script with error code Dismiss 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 Automatic exit
Bash Script Stop On Error
from bash shell script on error up vote 276 down vote favorite 48 I've been writing some shell script and I would find it useful if there was the ability to halt the execution of said shell script if any of the commands failed. See below for an example: #!/bin/bash cd some_dir ./configure --some-flags make make install So in this case if the script
Bash Exit Status Variable
can't change to the indicated directory then it would certainly not want to do a ./configure afterwards if it fails. Now I'm well aware that I could have an if check for each command (which I think is a hopeless solution), but is there a global setting to make the script exit if one of the commands fails? bash shell exit share|improve this question edited Mar 29 '15 at 23:26 asked May 20 '10 at 4:21 radman 5,57462242 I did have a quick look for duplicates and couldn't find any. –radman May 20 '10 at 4:22 1 answer goes to Adam for the detail regarding set -e (which is exactly wanted). Also thanks to a_m0d for the info on traps (though not 100% relevant). –radman May 20 '10 at 5:07 add a comment| 7 Answers 7 active oldest votes up vote 440 down vote accepted Use the set -e builtin: #!/bin/bash set -e # Any subsequent(*) commands which fail will cause the shell script to exit immediately Alternatively, you can pass -e on the command line: bash -e my_script.sh You can also disable this behavior with se
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 process.Every command exit bash shell returns an exit status (sometimes referred to as a return statusBash Exit Function
or exit code). A successful command returns a 0, while an unsuccessful one returns a non-zero value that bash if exit code 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 http://stackoverflow.com/questions/2870992/automatic-exit-from-bash-shell-script-on-error 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 http://tldp.org/LDP/abs/html/exit-status.html 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.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 unusual ways. It's possible to http://www.davidpashley.com/articles/writing-robust-shell-scripts/ 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, http://unix.stackexchange.com/questions/241178/how-can-i-get-this-script-to-error-exit-based-on-result-of-for-loop 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 system documentation rather than making a smaller chroot. So what can on error 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 bash that it should exit the script if any statement returns a non-true return value. The benefit exit on error 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. If you would like this to fail, then you can use set -o pipefail to make it fail. Program defensively - expect the unexpected Your script should take into account of the unexpected, like files
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 can I get this script to error exit based on result of for loop? up vote 4 down vote favorite 1 I have a bash script which uses set -o errexit so that on error the entire script exits at the point of failure. The script runs a curl command which sometimes fails to retrieve the intended file - however when this occurs the script doesn't error exit. I have added a for loop to pause for a few seconds then retry the curl command use false at the bottom of the for loop to define a default non-zero exit status - if the curl command succeeds - the loop breaks and the exit status of the last command should be zero. #! /bin/bash set -o errexit # ... for (( i=1; i<5; i++ )) do echo "attempt number: "$i curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim if [ -f ~/.vim/autoload/pathogen.vim ] then echo "file has been retrieved by curl, so breaking now..." break; fi echo "curl'ed file doesn't yet exist, so now will wait 5 seconds and retry" sleep 5 # exit with non-zero status so main script will errexit false done # rest of script ..... The problem is when the curl command fails, the loop retries the command five times - if all attempts are unsuccessful the for loop finis