Linux Script Error
Contents |
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 of subtle effects which result in scripts failing in unusual ways. It's possible to write scripts
Shell Script Exit Code
which minimise these problems. In this article, I explain several techniques for writing robust bash scripts. shell script exit on 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 bash if exit code ... rm -rf $chroot/usr/share/doc If you ran the 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?
Bash Catch Error
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= 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
Linux Kernel Error Codes
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 the error checking, but I recommend you use this sparingly. set +e command1 command2 set -e On a slightly related note, by default bash takes the error status of the last item in a pipeline, which may not be what you want. For example, false | true will be considered to have succeeded. If you would like this to fail, then you can use set -o pipefail to make it fail. Program defensively - expect the unexpected Your script should take into account of the unexpected, like files missing or directories not being created. There are several things you can do
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 bash if exit code not 0 about Stack Overflow the company Business Learn more about hiring developers or posting ads
Bash Trap
with us Unix & Linux Questions Tags Users Badges Unanswered Ask Question _ Unix & Linux Stack Exchange is a question and exit bash shell 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 http://www.davidpashley.com/articles/writing-robust-shell-scripts/ answers are voted up and rise to the top How to catch an error in a linux bash script? up vote 5 down vote favorite 1 I made the following script: # !/bin/bash # OUTPUT-COLORING red='\e[0;31m' green='\e[0;32m' NC='\e[0m' # No Color # FUNCTIONS # directoryExists - Does the directory exist? function directoryExists { cd $1 if [ $? = 0 ] then echo -e "${green}$1${NC}" else echo -e "${red}$1${NC}" fi } http://unix.stackexchange.com/questions/97101/how-to-catch-an-error-in-a-linux-bash-script # EXE directoryExists "~/foobar" directoryExists "/www/html/drupal" The script works, but beside my echoes, there is also the output when cd $1 fails on execution. testscripts//test_labo3: line 11: cd: ~/foobar: No such file or directory Is it possible to catch this? bash shell shell-script error-handling share|improve this question edited Oct 22 '13 at 22:58 Gilles 372k696761127 asked Oct 22 '13 at 10:29 Thomas De Wilde 28114 Just an FYI, you can also do this a lot simpler; test -d /path/to/directory ( or [[ -d /path/to/directory ]] in bash ) will tell you whether a given target is a directory or not, and it will do it quietly. –Patrick Oct 22 '13 at 12:36 @Patrick, that just tests if it's a directory, not if you can cd into it. –Stéphane Chazelas Oct 22 '13 at 12:54 @StephaneChazelas yes. The function name is directoryExists. –Patrick Oct 22 '13 at 13:57 add a comment| 5 Answers 5 active oldest votes up vote 4 down vote accepted Your script changes directories as it runs, which means it won't work with a series of relative pathnames. You then commented later that you only wanted to check for directory existence, not the ability to use cd, so answers don't need to use c
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 http://stackoverflow.com/questions/64786/error-handling-in-bash 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 6.2 million http://phaq.phunsites.net/2010/11/22/trap-errors-exit-codes-and-line-numbers-within-a-bash-script/ programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Error handling in BASH up vote 144 down vote favorite 110 What is your favorite method to handle errors in BASH? exit code 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 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 shell script exit command line ($0). # Reference: This was copied from
to breach system security Categories Bits and Bytes (20) Editors (1) HA (10) Hacks (10) Hardware (19) HowTo's (12) Memos (3) Networking (27) Check Point (4) Cisco (6) IPv6 (2) Operating Systems (62) Debian GNU/Linux (7) DOS (2) FreeBSD (26) OS X (19) RHEL (2) Windows (8) Programming (38) Perl (12) PHP (6) RegExp (2) Scripting (13) Shells (5) TCL (1) Publications (6) Security (4) Utilities (11) Virtualization (21) jails (6) VirtualBox (5) Virtuozzo (2) VMware (4) Xen (3) Recent Tweets There are no recent tweets. November 22, 2010 Trap Errors, Exit Codes and Line Numbers within a Bash script (and some output redirection, too) Posted by: admin : Category: Programming, Scripting, Shells A discussion today was about error handling in shell scripts, Bash in particular. Well, we all know about the usual knitpicks about error handling and the possible consequences in not doing so properly 😉 The most simplistic approach in error handling are of course control structures to check the return value: some arbitrary command if [ "$?" = "0" ]; then do something else do something else fi Or even more simplistic: some arbitrary command && { do something; } || { do something else; } There are more ways to do this. But they all have in common, that it is hard to trap and trace unknown errors, especially if the script runs unattended. If you want to do some logging and tracing, then you would need to implement a routine which you would need to add to each and every block, to ensure you don't miss some particular important information. Even if it is a simple function for error reporting, let's call it error_reporter, you would end up with something like this: some arbitrary command if [ "$?" = "0" ]; then do something error_reporter args else do something else error_reporter args fi But sometimes it's better to have an error handler, which is able to catch errors and do some special actions, while still allowing your script to continue within the normal flow. Even better if that particular error handler also catches and notifies upon yet-unknown and never discovered errors. Let's assume a script, which is trying to delete a directory. For the sake of this example, the directory MUST NOT exist, so the script effectively fails upon execution. We handle the error simply by checking on the non-zero exit co