Bash Error Exit Function
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
Bash Error Exit Code
site About Us Learn more about Stack Overflow the company Business Learn more bash exit status variable about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x
Bash Return Vs Exit
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 Error handling bash trap error in BASH up vote 143 down vote favorite 110 What is your favorite method to handle errors in BASH? The best example of handling errors in BASH I have found on the web was written by William Shotts, Jr at http://www.linuxcommand.org. William Shotts, Jr suggests using the following function for error handling in BASH: #!/bin/bash # A slicker error handling routine # I put bash traps a variable in my scripts named PROGNAME which # holds the name of the program being run. You can get this # value from the first item on the command line ($0). # Reference: This was copied from
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
Shell Script Exit Code
of subtle effects which result in scripts failing in unusual ways. It's possible bash if exit code to write scripts which minimise these problems. In this article, I explain several techniques for writing robust bash scripts.
Bash Catch Error
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/64786/error-handling-in-bash 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 $chroot= http://www.davidpashley.com/articles/writing-robust-shell-scripts/ 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 turn off
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 http://tldp.org/LDP/abs/html/exit-status.html 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 http://www.fvue.nl/wiki/Bash:_Error_handling 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 exit code 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.error yourself if subshell fails 3.1.1 Example 1 3.1.2 Example 2 4 Caveat 2: `Exit on error' not exitting subshell on error 4.1 Solution: Use logical operators (&&, ||) within subshell 4.1.1 Example 5 Caveat 3: `Exit on error' not exitting command substition on error 5.1 Solution 1: Use logical operators (&&, ||) within command substitution 5.2 Solution 2: Enable posix mode 6 The tools 6.1 Exit on error 6.1.1 Specify `bash -e' as the shebang interpreter 6.1.1.1 Example 6.1.2 Set ERR trap to exit 6.1.2.1 Example 7 Solutions revisited: Combining the tools 7.1 Executed in subshell, trap on exit 7.1.1 Rationale 7.2 Sourced in current shell 7.2.1 Todo 7.2.2 Rationale 7.2.2.1 `Exit' trap in sourced script 7.2.2.2 `Break' trap in sourced script 7.2.2.3 Trap in function in sourced script without `errtrace' 7.2.2.4 Trap in function in sourced script with 'errtrace' 7.2.2.5 `Break' trap in function in sourced script with `errtrace' 8 Test 9 See also 10 Journal 10.1 20060524 10.2 20060525 11 Comments Problem I want to catch errors in bash script using set -e (or set -o errexit or trap ERR). What are best practices? To -e or not to to -e? Opinions differ about whether it's wise to use set -e, because of its seemingly non-intuitive problems... In favour of -e: Use set -e - Writing Robust Bash Shell Scripts - David Pashley Doubtful about -e: Why doesn't set -e (or set -o errexit, or trap ERR) do what I expected? - BashFAQ/105 - Greg's Wiki Solutions See #Solutions revisited: Combining the tools for detailed explanations. If the script is executed in a subshell, it's relative easy: You don't have to worry about backing up and restoring shell options and shell traps, because they're automatically restored when you exit the subshell. Executed in subshell, exit on error Example script: #!/bin/bash -eu # -e: Exit immediately if a command exits with a non-zero status. # -u: Treat unset variables as an error when substituting. (false) # Caveat 1: If an error occurs in a subshell, it isn't detected (false) || false # Solution: If you want to exit, you have to detect the error yourself (false; true) || false # Caveat 2: The return status of the ';' separated list is `true' (false && true) || false # Solution: If you want to control the last command executed, use `&&' See also #Caveat 1: `Exit on error' ignoring subshell exit status Executed in subshell, trap error