Bash Terminate 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 bash exit on error with message more about Stack Overflow the company Business Learn more about hiring developers or posting shell script error handling ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community
Bash Errexit
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 Stop on first error [duplicate] up vote 115 down vote
Bash Set +e
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 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 bash set o 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 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 fold
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
Bash Fail Command
us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow bash exit on error code 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 bash trap How to exit if a command failed? up vote 99 down vote favorite 30 I am a noob in shell-scripting. I want to print a message and exit my script if a command fails. I've tried: my_command && (echo 'my_command failed; exit) http://stackoverflow.com/questions/3474526/stop-on-first-error but it does not work. It keeps executing the instructions following this line in the script. I'm using Ubuntu and bash. linux bash exit exitstatus share|improve this question edited Mar 21 at 4:35 Benjamin W. 8,066112043 asked Sep 29 '10 at 14:32 user459246 574157 1 Did you intend the unclosed quote to be a syntax error that would cause fail/exit? if not, you should close the quote in your example. –hobs Dec 29 '15 at 18:02 add a comment| 6 Answers 6 active oldest http://stackoverflow.com/questions/3822621/how-to-exit-if-a-command-failed votes up vote 194 down vote accepted Try: my_command || { echo 'my_command failed' ; exit 1; } Four changes: Change && to || Use { } in place of ( ) Introduce ; after exit and spaces after { and before } Since you want to print the message and exit only when the command fails ( exits with non-zero value) you need a || not an &&. cmd1 && cmd2 will run cmd2 when cmd1 succeeds(exit value 0). Where as cmd1 || cmd2 will run cmd2 when cmd1 fails(exit value non-zero). Using ( ) makes the command inside them run in a sub-shell and calling a exit from there causes you to exit the sub-shell and not your original shell, hence execution continues in your original shell. To overcome this use { } The last two changes are required by bash. share|improve this answer edited Jan 30 '13 at 17:16 answered Sep 29 '10 at 14:35 codaddict 249k50361440 2 It does appear to be "reversed". If a function "succeeds" it returns 0 and if it "fails" it returns non-zero therefore && might be expected to evaluate when the first half returned non-zero. That does not mean the answer above is incorrect - no it is correct. && and || in scripts work based on success not on the return value. –CashCow Jul 20 '12 at 10:43 6 It seems reversed, but read it out and it makes sense: "do this command (successfully)" OR "print this error an
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 http://unix.stackexchange.com/questions/82224/how-to-make-bash-abort-the-execution-of-a-script-on-syntax-error 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 on error 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 bash exit on 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 something to do with (( )). But I find it still unreasonable to continue executing afte a syntax error. No, (( )) makes no difference! It behaves bad even without the arithmetic test! Just a simple, basic script: #!/bin/bash set -e # exit on any error readonly a=(1 2) # A syntax error is here: echo "${a[#]}" 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 (erro