Error Trapping In Unix Shell Script
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 with signals. Consider the following program: #!/bin/bash echo "this script will
Error Handling In Unix Shell Script
endlessly loop until you stop it" while true; do : # Do nothing done After unix error handling you launch this script it will appear to hang. Actually, like most programs that appear to hang, it is really stuck inside a shell script exception handling loop. In this case, it is waiting for the true command to return a non-zero exit status, which it never does. Once started, the script will continue until bash receives a signal that will stop it. You
Trap Command In Unix Shell Script
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 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
How To Handle Special Characters In Unix Shell Script
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 temporary file. Good practice would dictate that we delete the temporary file $TEMP_FILE when the script terminates. This is easily accomplished by adding the following to the end of the script: rm $TEMP_FILE This would seem to solve the problem, but what happens if the user types ctrl-c when the "Print file? [y/n]:" prompt appears? The script will terminate at the read command and the rm command is never e
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 file handling in shell script unix site About Us Learn more about Stack Overflow the company Business Learn more
For Loop In A Shell Script
about hiring developers or posting ads with us Unix & Linux Questions Tags Users Badges Unanswered Ask Question _ Unix korn shell error handling & 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: Sign up Here's how it works: http://linuxcommand.org/wss0160.php 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 the directory exist? function directoryExists { cd $1 if http://unix.stackexchange.com/questions/97101/how-to-catch-an-error-in-a-linux-bash-script [ $? = 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 371k696751126 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 w
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 http://stackoverflow.com/questions/64786/error-handling-in-bash 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 http://askubuntu.com/questions/509795/how-to-write-a-portable-shell-script-with-error-handling of 4.7 million 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 109 What is your favorite method to shell script 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 a variable in my scripts named PROGNAME which # holds the name of the program being run. You can get this # in unix shell value from the first item on the command line ($0). # Reference: This was copied from
communities company blog Stack Exchange Inbox Reputation and Badges sign up log in tour help Tour Start 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 Ask Ubuntu Questions Tags Users Badges Unanswered Ask Question _ Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute: 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 write a portable shell script with error handling up vote 5 down vote favorite I have had relatively good success at writing portable, full-featured shell shell scripts by using /bin/sh, for example: #!/bin/sh trap 'echo "Error on line $LINENO"; exit 1' ERR while read LINE do echo "+ $LINE" done < file.txt this works on BSD because /bin/sh is typically an alias to ksh $ ls -li /bin/{sh,ksh} 26768 -r-xr-xr-x 3 root bin 418612 Jun 18 17:41 /bin/ksh 26768 -r-xr-xr-x 3 root bin 418612 Jun 18 17:41 /bin/sh While on MacOS and many Linux distros simply symlink /bin/sh to bash $ ls -li /bin/{sh,bash} 17170438 -rwxr-xr-x 1 root root 938832 Jul 18 2013 /bin/bash 17170540 lrwxrwxrwx 1 root root 4 Feb 19 12:42 /bin/sh -> bash If these two conditions are true then I can use a relatively rich feature set common to both KSH and BASH. On Ubuntu the default shell only supports POSIX features. Is it still possible to write a cross-platform shell script that also incorporates error handling? Edit (2014-08-18) The solution I came up with was to auto-upgrade the script if the given shell doesn't understand the ERR trap #!/bin/sh trap 'echo "Error on line $LINENO"; exit 1' ERR || exec bash $0 "$@" This allows me to write reasonably portable shell scripts that only require bash if the default shell doesn't support KSH features. bash share|improve this question edited Dec 9 '14 at 13:15 asked Aug 11 '14 at 13:57 eradman 21427 @dan08 well, there are questions kept here, that are suitable for Ask Ubuntu far less than this one. –user280493 Aug 11 '14 at 14:20 3 This question is perfectly on topic. –Seth♦ Aug 11 '14 at 14:43 @Seth While it is on topic, portability is an oft-raised issue on Unix & Linux, and (IMO) the real