Bash Error Handling Best Practices
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 site error handling best practices c# About Us Learn more about Stack Overflow the company Business Learn more about python error handling best practices hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join
Ssis Error Handling Best Practices
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 in BASH
Angularjs Error Handling Best Practices
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 a variable in my javascript error handling best practices 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
Java Error Handling Best Practices
own. Unfortunately shell scripts are full of subtle effects which result in php error handling best practices scripts failing in unusual ways. It's possible to write scripts which minimise these problems. In this article, I bash throw error 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 set? I know I have, http://stackoverflow.com/questions/64786/error-handling-in-bash 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 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 http://www.davidpashley.com/articles/writing-robust-shell-scripts/ 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 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
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 http://unix.stackexchange.com/questions/8759/best-practice-to-use-in-bash about Stack Overflow the 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 http://askubuntu.com/questions/509795/how-to-write-a-portable-shell-script-with-error-handling 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: Anybody can ask a question Anybody can answer The best error handling answers are voted up and rise to the top Best practice to use $? in bash? up vote 7 down vote favorite 9 When I read this answer about $? another question comes to mind. Is there any best practice for how to use $? in bash? Let's have a example: We have a linear script and I we would like to know that all the command was executed ok. Do error handling best you think it is ok to call a small function (let's call it "did_it_work"), to check the error code and break if it's not. #!/bin/bash function did_it_work { code=$1 if [ "$code" -ne "0" ] then echo "Error failure: code $code " exit 1 fi } dir=some/path mkdir -p $dir did_it_work $? cd $dir did_it_work $? run_some_command did_it_work $? This approach of course means that I have to manually solve the problem if there is any and rerun the script. Do you think this is a good idea or is there some other best practice to do this? /Thanks shell bash error-handling share|improve this question edited Apr 3 '11 at 20:09 Gilles 369k666681119 asked Mar 7 '11 at 16:35 Johan 2,53311326 add a comment| 3 Answers 3 active oldest votes up vote 11 down vote accepted One common way is: die() { echo "$*" 1>&2 exit 1 } then you use it like this: mkdir -p some/path || die "mkdir failed with status $?" Or if you want it to include the exit status, you could change it to: die() { echo "FATAL ERROR: $* (status $?)" 1>&2 exit 1 } and then using it is a bit easier: mkdir -p some/path || die "mkdir failed" Just in case you hav
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 tra