Bash Error Exit Code
Contents |
Ramey
The exit command terminates a script, just as in a C program. It can also return linux bash return code a value, which is available to the script's parent process.Every linux exit value command returns an exit status (sometimes referred to as a return status or exit codeBash Get 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
Bash Last Exit Code
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 bash exit code of command 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.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 a good program and a poor one is often measured in terms of the
Bash Exit Code 143
program's robustness. That is, the program's ability to handle situations in which something goes wrong. bash exit code pipe Exit status As you recall from previous lessons, every well-written program returns an exit status when it finishes. If a program finishes successfully, bash exit code 127 the exit status will be zero. If the exit status is 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 http://tldp.org/LDP/abs/html/exit-status.html 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: # 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 http://linuxcommand.org/wss0150.php 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 following: [me] $ true; echo $? 0 [me] $ false; echo $? 1 The true and false commands are programs that do nothing except return an exit status of zero and one, respectively. Using them, we can see how the $? environment variable contains the exit status of the previous program. So to check the exit status, we could write the script this way: # Check the exit status cd $some_directory if [ "$?" = "0" ]; then rm * else echo "Cannot change d
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 http://stackoverflow.com/questions/4381618/exit-a-script-on-error more about Stack Overflow the company Business Learn more about hiring developers or http://stackoverflow.com/questions/90418/exit-shell-script-based-on-process-exit-code 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 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 exit code favorite 7 I'm building a Shell Script that has 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 bash exit code Nov 9 '15 at 14:39 NargothBond 4872722 asked Dec 7 '10 at 21:10 Nathan 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
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 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 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Exit Shell Script Based on Process Exit Code up vote 238 down vote favorite 76 I have a shell script that executes a number of commands. How do I make the shell script exit if any of the commands exit with a non-zero exit code? bash shell share|improve this question edited Jul 31 '11 at 19:12 Cerin 16.3k36138273 asked Sep 18 '08 at 6:03 Mark Roddy 10.7k95161 1 I answered assuming you're using bash, but if it's a very different shell can you specify in your post? –Martin W Sep 18 '08 at 6:11 add a comment| 9 Answers 9 active oldest votes up vote 312 down vote accepted After each command, the exit code can be found in the $? variable so you would have something like: ls -al file.ext rc=$?; if [[ $rc != 0 ]]; then exit $rc; fi You need to be careful of piped commands since the $? only gives you the return code of the last element in the pipe so, in the code: ls -al file.ext | sed 's/^/xx: /" will not return an error code if the file doesn't exist (since the sed part of the pipeline actually works, returning 0). The bash shell actually provides an array which can assist in that case, that being PIPESTATUS. This array has one element for each of the pipeline components, that you can access individually like ${PIPESTATUS[0]}: pax> false | true ; echo ${PIPESTATUS[0]} 1 Note that this is getting you the result of the false command, not the entire pipeline. You can also get the entire list to process as you see fit: pax> false | true | false; echo ${PIPESTATUS[*]} 1 0 1 If you wanted to get the largest error code from a pipeline, you could use something like: true | true | false | true | false rcs=${PIPESTATUS[*]}; rc=0; for i in ${rcs}; do rc=$(($i > $rc ? $i : $rc)); done echo $rc This goes through each of the PIPESTATUS elements in turn, storing it in rc if it was greater than the previous rc value. share|improve this answer edited Jan 7 at 1:24 answered Sep 18 '08 at 6:08 paxdia