Bash Break If 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 bash break out of if more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users
Bash Break Out Of If Statement
Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping bash script exit if error each 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 bash if error code 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.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
Bash Check If Error
question. add a 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 69578 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 eq
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 bash check if error occurred status (sometimes referred to as a return status or exit code). bash for loop break A successful command returns a 0, while an unsuccessful one returns a non-zero value that usually can be interpreted asBash If Exist
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 http://stackoverflow.com/questions/3474526/stop-on-first-error 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 http://tldp.org/LDP/abs/html/exit-status.html 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.current loop before its normal ending. This is done when you don't know in advance how many times the loop will have to execute, for instance because it is dependent on user http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_09_05.html input.
The example below demonstrates a while loop that can be interrupted. This is a slightly improved version of the wisdom.sh script from Section 9.2.2.3. #!/bin/bash # This script provides wisdom # You can now exit in a decent way. FORTUNE=/usr/games/fortune while true; do echo "On which topic do you want advice?" echo "1. politics" echo "2. startrek" echo "3. kernelnewbies" echo if error "4. sports" echo "5. bofh-excuses" echo "6. magic" echo "7. love" echo "8. literature" echo "9. drugs" echo "10. education" echo echo -n "Enter your choice, or 0 for exit: " read choice echo case $choice in 1) $FORTUNE politics ;; 2) $FORTUNE startrek ;; 3) $FORTUNE kernelnewbies ;; 4) echo "Sports are a waste of time, energy and money." echo "Go back to your keyboard." bash break out echo -e "\t\t\t\t -- \"Unhealthy is my middle name\" Soggie." ;; 5) $FORTUNE bofh-excuses ;; 6) $FORTUNE magic ;; 7) $FORTUNE love ;; 8) $FORTUNE literature ;; 9) $FORTUNE drugs ;; 10) $FORTUNE education ;; 0) echo "OK, see you!" break ;; *) echo "That is not a valid choice, try a number from 0 to 10." ;; esac done Mind that break exits the loop, not the script. This can be demonstrated by adding an echo command at the end of the script. This echo will also be executed upon input that causes break to be executed (when the user types "0").In nested loops, break allows for specification of which loop to exit. See the Bash info pages for more.