Bash Error Status
Contents |
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 bash return code exit status (sometimes referred to as a return status or exit linux exit status code). A successful command returns a 0, while an unsuccessful one returns a non-zero value that usually canBash Script Exit Code
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 itselfLinux Exit Code
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 exit code of 0 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.and Signals and Traps (Oh My!) - Part 1 by William Shotts, Jr. In this lesson, we're going to look at handling errors during the execution of your scripts. The difference between
Bash Exit Status Variable
a good program and a poor one is often measured in terms bash quit on error of the program's robustness. That is, the program's ability to handle situations in which something goes wrong. Exit status bash error message As you recall from previous lessons, every well-written program returns an exit status when it finishes. If a program finishes successfully, the exit status will be zero. If the exit status is http://tldp.org/LDP/abs/html/exit-status.html anything other than zero, then the program failed in some way. It is very important to check the exit status of programs you call in your scripts. It is also important that your scripts return a meaningful exit status when they finish. I once had a Unix system administrator who wrote a script for a production system containing the following 2 lines of code: http://linuxcommand.org/wss0150.php # Example of a really bad idea cd $some_directory rm * Why is this such a bad way of doing it? It's not, if nothing goes wrong. The two lines change the working directory to the name contained in $some_directory and delete the files in that directory. That's the intended behavior. But what happens if the directory named in $some_directory doesn't exist? In that case, the cd command will fail and the script executes the rm command on the current working directory. Not the intended behavior! By the way, my hapless system administrator's script suffered this very failure and it destroyed a large portion of an important production system. Don't let this happen to you! The problem with the script was that it did not check the exit status of the cd command before proceeding with the rm command. Checking the exit status There are several ways you can get and respond to the exit status of a program. First, you can examine the contents of the $? environment variable. $? will contain the exit status of the last command executed. You can see this work with the foll
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 http://stackoverflow.com/questions/4381618/exit-a-script-on-error company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 http://linuxcommando.blogspot.com/2008/03/how-to-check-exit-status-code.html million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Exit a Script On Error up vote 43 down vote favorite 7 I'm building a Shell Script that has exit code a if function like this one: if jarsigner -verbose -keystore $keyst -keystore $pass $jar_file $kalias then echo $jar_file signed sucessfully else echo ERROR: Failed to sign $jar_file. Please recheck the variables fi ... I want the execution of the script to finish after displaying the error message. How I can do this? bash exit shell share|improve this question edited Nov 9 '15 at 14:39 NargothBond 4872722 asked Dec 7 '10 at 21:10 Nathan bash error status Campos 10.2k37149260 add a comment| 5 Answers 5 active oldest votes up vote 33 down vote accepted Are you looking for exit? This is the best bash guide around. http://tldp.org/LDP/abs/html/ In context: if jarsigner -verbose -keystore $keyst -keystore $pass $jar_file $kalias then echo $jar_file signed sucessfully else echo ERROR: Failed to sign $jar_file. Please recheck the variables 1>&2 exit 1 # terminate and indicate error fi ... share|improve this answer edited Apr 23 '14 at 9:13 MattBianco 795721 answered Dec 7 '10 at 21:13 Byron Whitlock 35k1985141 2 If you like the ABS, you'll love the BashGuide, BashFAQ and BashPitfalls. –Dennis Williamson Dec 8 '10 at 4:45 add a comment| up vote 167 down vote If you put set -e in a script, the script will terminate as soon as any command inside it fails (i.e. as soon as any command returns a nonzero status). This doesn't let you write your own message, but often the failing command's own messages are enough. The advantage of this approach is that it's automatic: you don't run the risk of forgetting to deal with an error case. Commands whose status is tested by a conditional (such as if, && or ||) do not terminate the script (otherwise the conditional would be pointless). An idiom for the occasional c
Resources Polls Contact Me / Advertising Search This Blog Monday, March 24, 2008 How to check the exit status code When a command finishes execution, it returns an exit code. The exit code is not displayed on the screen by default. To examine the exit code, you need to examine a special variable, "$?" Say, you are searching for a string in a text file. $ grep x1y2z3 somefile.txt $ The standard output of the command returns null, which is a pretty good indication that the string cannot be found in the file. But what if you embed the grep command in a script? How can you tell if the string is found or not? Checking the exit code will tell you. Let's first try it out interactively. $ grep x1y2z3 somefile.txt $ echo $? 1 Note that in bash, the exit status is 0 if the command succeeded, and 1 if failed. For grep, 0 means that the string was found, and 1 (or higher), otherwise. To check the exit status in a script, you may use the following pattern: somecommand argument1 argument2 RETVAL=$? [ $RETVAL -eq 0 ] && echo Success [ $RETVAL -ne 0 ] && echo Failure Posted by Peter Leung at 7:50 PM Email ThisBlogThis!Share to TwitterShare to FacebookShare to Pinterest 4 comments: Mad MAx said... $RETVAL=$?is not correct.when you assign a value to a variable, in bash, you can't prepend variable name with $.The correct form isRETVAL=$?regards, Mad Max. August 4, 2009 at 3:07 AM Peter Leung said... Thanks, Mad MAx.I made the correction.Peter August 4, 2009 at 8:50 PM Anonymous said... You can also just avoid the RETVAL altogether and use the "||" or "&&" operands which are called when the command on the left returns 1 or 0 respectively, e.g.# grep returns 1, e.g. no matchgrep foo /tmp/bar.txt || echo "text not found"# grep returns 0, e.g. matchgrep baz /tmp/bar.txt && echo "found it!" April 25, 2011 at 2:56 PM Wang said... Thanks for sharing, and thanks for the above comment. July 31, 2012 at 11:59 AM P