Error Shell Script
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 shell script error handling difference between a good program and a poor one is often measured shell script error checking in terms of the program's robustness. That is, the program's ability to handle situations in which something goes wrong.
Shell Script Error Code
Exit status As you recall 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 the
Shell Script Error Bad Interpreter
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 meaningful exit status when they finish. I once had a Unix system administrator who wrote a script for a production system containing the shell script error code 1 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 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 las
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
Shell Script Error Exit
this site About Us Learn more about Stack Overflow the company Business Learn shell script error command not found more about hiring developers or posting ads with us Unix & Linux Questions Tags Users Badges Unanswered Ask Question shell script error output _ 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: Sign up Here's how http://linuxcommand.org/wss0150.php 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 the directory exist? function directoryExists { http://unix.stackexchange.com/questions/97101/how-to-catch-an-error-in-a-linux-bash-script 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 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
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/12335356/how-to-handle-error-exception-in-shell-script Overflow the company Business Learn more about hiring developers or posting ads with us https://www.dssw.co.uk/blog/2012-10-08-how-to-add-error-checking-to-your-shell-scripts/ Stack Overflow Questions Jobs Documentation Tags Users 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 minute: Sign up How to handle error/exception in shell script? up vote 6 down vote favorite 1 Below is shell script 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 then I don't know what's has happened? As after the above script is executed, there are some other shell script error 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,11044162271 @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 time for sure. :) –lining Sep 8 '12 at 23:50 add a comment| 3 Answers 3 active oldest votes up vote 8 down vote accepted You can check for the exit status of each command,
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 [ "$?" != "0" ]; then echo "[Error] copy failed!" 1>&2 exit 1 fi We have added an if/fi block below the cp line. The new block checks the special variable $? to see if it equals 0 or not. Unix programs should return 0 if they completed successfully. Any other value means something went wrong. The exact meaning of the returned value is frequently documented in the program's man page. If an error is d