Error Handling In Unix
Contents |
and Signals and Traps (Oh My!) - Part 1 by William Shotts, Jr. In this lesson, we're going to look at handling errors during the execution of your scripts. The difference between a good program and a poor one is often measured in terms of the program's robustness. That is,
Error Handling In Unix Script
the program's ability to handle situations in which something goes wrong. Exit status As you recall error handling in unix shell script from previous lessons, every well-written program returns an exit status when it finishes. If a program finishes successfully, the exit status will be zero. If unix error codes the exit status is anything other than zero, then the program failed in some way. It is very important to check the exit status of programs you call in your scripts. It is also important that your scripts return a
Exception Handling In Unix Shell Script
meaningful exit status when they finish. I once had a Unix system administrator who wrote a script for a production system containing the following 2 lines of code: # Example of a really bad idea cd $some_directory rm * Why is this such a bad way of doing it? It's not, if nothing goes wrong. The two lines change the working directory to the name contained in $some_directory and delete the files in that directory. That's the intended behavior. But what happens
Sql Error Handling
if the directory named in $some_directory doesn't exist? In that case, the cd command will fail and the script executes the rm command on the current working directory. Not the intended behavior! By the way, my hapless system administrator's script suffered this very failure and it destroyed a large portion of an important production system. Don't let this happen to you! The problem with the script was that it did not check the exit status of the cd command before proceeding with the rm command. Checking the exit status There are several ways you can get and respond to the exit status of a program. First, you can examine the contents of the $? environment variable. $? will contain the exit status of the last command executed. You can see this work with the following: [me] $ true; echo $? 0 [me] $ false; echo $? 1 The true and false commands are programs that do nothing except return an exit status of zero and one, respectively. Using them, we can see how the $? environment variable contains the exit status of the previous program. So to check the exit status, we could write the script this way: # Check the exit status cd $some_directory if [ "$?" = "0" ]; then rm * else echo "Cannot change directory!" 1>&2 exit 1 fi In this version, we examine the exit status of the cd command and if it's not zero, we print an err
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 dos error handling Learn more about hiring developers or posting ads with us Unix & Linux Questions Tags Users
Oracle Error Handling
Badges Unanswered Ask Question _ Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other windows script error handling 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 answers are voted up and rise to the top How to catch an http://linuxcommand.org/wss0150.php 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 } # EXE directoryExists "~/foobar" directoryExists "/www/html/drupal" The script works, but beside my echoes, there is also the output when http://unix.stackexchange.com/questions/97101/how-to-catch-an-error-in-a-linux-bash-script 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 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 cd at all. Revised. Using tput and colours from man terminfo: #!/bin/bash -u # OUTPUT-COLORING red=$( tput setaf 1 ) green=$( tput setaf 2 ) NC=$( tput setaf 0 ) # or perhaps: tput sgr0 # FUNCTIONS # direc
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 http://stackoverflow.com/questions/12335356/how-to-handle-error-exception-in-shell-script About Us Learn more about Stack 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 http://askubuntu.com/questions/509795/how-to-write-a-portable-shell-script-with-error-handling 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 How to handle error/exception in error handling shell script? up vote 6 down vote favorite 1 Below is my script that I am executing in the bash. And it works fine. fileexist=0 for i in $( ls /data/read-only/clv/daily/Finished-HADOOP_EXPORT_&processDate#.done); do mv /data/read-only/clv/daily/Finished-HADOOP_EXPORT_&processDate#.done /data/read-only/clv/daily/archieve-wip/ fileexist=1 done Problem Statement:- In my above shell script which has to be run daily using cron job, I don't have any error/exception handling mechanism. Suppose if anything gets wrong handling in unix then I don't know what's has happened? As after the above script is executed, there are some other scripts that will be dependent on the data provided by above script, so I always get's complaint from the other people who are depending on my script data that something wrong has happened. So is there any way I can get notified if anything wrong has happened in my script? Suppose if the cluster is having some maintenance and at that time I am running my script, so definitely it will be failing for sure, so can I be notified if my above scripts failed, so that I will be sure something wrong has happened. Hope my question is clear enough. Any thoughts will be appreciated. linux bash shell unix share|improve this question edited Sep 10 '12 at 2:52 asked Sep 8 '12 at 23:38 lining 6,10044162271 @Levon, Done. Now it's better? –lining Sep 8 '12 at 23:48 Yes, I think this looks much better and more readable, won't intimidate potential helpers away :) –Levon Sep 8 '12 at 23:49 Yup.. Thanks for the advice. I will make sure next tim
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