Linux Shell Script Stop 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 shell script error handling more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags bash exit on error with message Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, bash errexit helping each other. Join them; it only takes a minute: Sign up Stop on first error [duplicate] up vote 121 down vote favorite 29 Possible Duplicate: Automatic exit from bash shell script on error How can I have
Bash Set +e
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 share|improve this question asked Aug 13 '10 at 6:45 Matt Joiner 41.7k45217384 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 bash fail command 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 71072 times active 6 years ago Linked 283 Automatic exit from bash shell script on error 197 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 488How to iterate over arguments in bash script1231In the shell, what does “ 2>&1 ” mean?1025How do I split a string on a delimiter in Bash?236How to pass all arguments passed to my bash script to a function of mine?589How to set a vari
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 bash disable exit on error of subtle effects which result in scripts failing in unusual ways. It's possible
Bash Script Exit On Error
to write scripts which minimise these problems. In this article, I explain several techniques for writing robust bash scripts.
Bash Set O
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 http://stackoverflow.com/questions/3474526/stop-on-first-error 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 http://www.davidpashley.com/articles/writing-robust-shell-scripts/ $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 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 tu
Get Kubuntu Get Xubuntu Get Lubuntu Get UbuntuStudio Get Mythbuntu Get Edubuntu Get Ubuntu-GNOME Get UbuntuKylin Ubuntu Code of Conduct Ubuntu https://ubuntuforums.org/showthread.php?t=1008259 Wiki Community Wiki Other Support Launchpad Answers Ubuntu IRC Support AskUbuntu Official http://tldp.org/LDP/abs/html/exit-status.html Documentation User Documentation Social Media Facebook Twitter Useful Links Distrowatch Bugs: Ubuntu PPAs: Ubuntu Web Upd8: Ubuntu OMG! Ubuntu Ubuntu Insights Planet Ubuntu Activity Page Please read before SSO login Advanced Search Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk [SOLVED] on error Make shell script exit as soon as a command in it returns an error? Having an Issue With Posting ? Do you want to help us debug the posting issues ? < is the place to report it, thanks ! Results 1 to 8 of 8 Thread: [SOLVED] Make shell script exit as soon as a command in it exit on error returns an error? Thread Tools Show Printable Version Subscribe to this Thread… Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode December 11th, 2008 #1 jasper.davidson View Profile View Forum Posts Private Message 5 Cups of Ubuntu Join Date Jul 2008 Beans 26 [SOLVED] Make shell script exit as soon as a command in it returns an error? I would like to make my shell script so that if any of the commands in it return an error, the script immmediately exits without executing the rest of the commands. What might be the best way of doing this? One way that seems to work is: Code: #!/bin/bash cmd1 || exit 1 cmd2 || exit 1 cmd3 || exit 1 etc. In other words, putting "|| exit 1" at the end of each executed command. Is there a better way I hope? Thanks in advance. Adv Reply December 11th, 2008 #2 dwhitney67 View Profile View Forum Posts Private Message Tolerant of Ubuntu Join Date Jun 2007 Location Maryland, US Beans 6,2
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 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.