Bash Script Command Error Handling
Contents |
and Signals and Traps (Oh, My!) - Part 2 by William Shotts, Jr. Errors are not the only way that a script can terminate unexpectedly. You also have to be concerned bash script catch error with signals. Consider the following program: #!/bin/bash echo "this script will endlessly shell script error handling loop until you stop it" while true; do : # Do nothing done After you launch this script it unix shell script error handling will appear to hang. Actually, like most programs that appear to hang, it is really stuck inside a loop. In this case, it is waiting for the true command to return a
Bash Throw Error
non-zero exit status, which it never does. Once started, the script will continue until bash receives a signal that will stop it. You can send such a signal by typing ctrl-c which is the signal called SIGINT (short for SIGnal INTerrupt). Cleaning up after yourself OK, so a signal can come along and make your script terminate. Why does it matter? Well, in korn shell error handling many cases it doesn't matter and you can ignore signals, but in some cases it will matter. Let's take a look at another script: #!/bin/bash # Program to print a text file with headers and footers TEMP_FILE=/tmp/printfile.txt pr $1 > $TEMP_FILE echo -n "Print file? [y/n]: " read if [ "$REPLY" = "y" ]; then lpr $TEMP_FILE fi This script processes a text file specified on the command line with the pr command and stores the result in a temporary file. Next, it asks the user if they want to print the file. If the user types "y", then the temporary file is passed to the lpr program for printing (you may substitute less for lpr if you don't actually have a printer attached to your system.) Now, I admit this script has a lot of design problems. While it needs a file name passed on the command line, it doesn't check that it got one, and it doesn't check that the file actually exists. But the problem I want to focus on here is the fact that when the script terminates, it leaves behind the tempor
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies shell script exit code of this site About Us Learn more about Stack Overflow the company
Bash If Exit Code
Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users
Shell Script Exit On Error
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 http://linuxcommand.org/wss0160.php minute: Sign up Error handling 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 http://stackoverflow.com/questions/64786/error-handling-in-bash 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 command line ($0). # Reference: This was copied from
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and http://unix.stackexchange.com/questions/97101/how-to-catch-an-error-in-a-linux-bash-script policies of this site About Us Learn more about Stack Overflow the https://www.dssw.co.uk/blog/2012-10-08-how-to-add-error-checking-to-your-shell-scripts/ company Business Learn 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: error handling 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 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 shell script error the directory exist? function directoryExists { cd $1 if [ $? = 0 ] then echo -e "${green}$1${NC}" else echo -e "${red}$1${NC}" fi } # 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 369k666681119 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
article shows how to add error checking to highlight problems you might otherwise miss. Access the shell on Mac via the Terminal.app Unlike more recently designed languages, shell script does not have an easy answer for error handling. There are no common exception handling routines or ways of wrapping up large blocks of script and asking for errors to fall through to a provided subroutine. Instead shell script asks you, the author, to check individual program exit codes and branch as needed in case of an error. In practice, this means your once short script is going to get a little longer and a little more involved. Unix Exit Status Each program you run through a shell script returns an exit status. This numeric status value tells the calling script if the program completed successfully or if an error was encountered. The exit status is not visible on the command line. This makes it difficult to tell if something went wrong just by looking at the textual output of a shell script. It is possible - even common - for scripts to print nothing and yet encounter multiple errors. Let's consider this simple script: #!/bin/sh cp /Volumes/Documents/criticalfile.txt /Volumes/BackUp/. This script does one thing; it copies a single file from one volume to another using the cp program. Thankfully the cp program is chatty and will print a message if an error is encountered. That is great for us reviewing the output visually, but for the shell running our script the error will go completely unnoticed. This is a problem if our script goes on to do more work, or if we want the script to robustly deal with errors. Let's add error checking to this simple script. #!/bin/sh cp /Volumes/Documents/criticalfile.txt /Volumes/BackUp/. if [ "$?"