Error Handling In Unix Script
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss error trapping in unix shell script the workings and policies of this site About Us Learn more about how to catch exception in shell script Stack Overflow the company Business Learn more about hiring developers or posting ads with us Unix & Linux
Sql Script Error Handling
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
Windows Script Error Handling
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 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' # dos script error handling 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 } # 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 371k686741124 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 di
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
Oracle Error Handling
company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions shell script exit code Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million bash if exit code programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Error handling in BASH up vote 143 down vote favorite 109 What is your favorite method to handle errors in http://unix.stackexchange.com/questions/97101/how-to-catch-an-error-in-a-linux-bash-script 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 # value from the first item http://stackoverflow.com/questions/64786/error-handling-in-bash 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 of subtle effects which result in scripts failing in unusual ways. http://www.davidpashley.com/articles/writing-robust-shell-scripts/ It's possible to write scripts which minimise these problems. In this article, I explain several techniques for writing robust bash scripts. Use set -u How often have you written a script that broke because a variable wasn't http://askubuntu.com/questions/509795/how-to-write-a-portable-shell-script-with-error-handling set? I know I have, many times. chroot=$1 ... 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 error handling 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= 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 error handling 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 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
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 experts are there, so it's more on-topic there. –muru Dec 9 '14 at 7:31 I removed the extra wording describing set +o posix in order to stay more on topic with writing a shell script that runs on Ubuntu –eradman Dec