Trapping errors in bash scripts is a little easier if you make use of the "trap function ERR" ability. From the bash man page:
If a sigspec is ERR, the command arg is executed whenever a simple command has a non-zero exit status. The ERR trap is not executed if the failed command is part of an until or while loop, part of an if statement, part of a && or || list, or if the command's return value is being inverted via !.
You'd use it in a script like this:
function error {
echo Error
exit 1
}
trap error ERR
# commands that may have non-zero exit follow
This post was prompted by the following newsgroup post:
From: "Wilson" <mr.sean.wil...@gmail.com>
Newsgroups: comp.os.linux.misc
Subject: Error handling in bash scripts
Date: 1 Feb 2005 16:35:35 -0800
Organization: http://groups.google.com
Lines: 39
Message-ID: <1107304535.119267.126770@c13g2000cwb.googlegroups.com>
NNTP-Posting-Host: 82.41.232.67
Mime-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1"
X-Trace: posting.google.com 1107304539 13058 127.0.0.1 (2 Feb 2005 00:35:39 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Wed, 2 Feb 2005 00:35:39 +0000 (UTC)
User-Agent: G2/0.2
Complaints-To: groups-abuse@google.com
Injection-Info: c13g2000cwb.googlegroups.com; posting-host=82.41.232.67;
posting-account=Fx6I8A0AAABwL32yCTr888uQZRlA8urB
Hi,
I'm trying my hand at bash script programming and was wondering how to
make my scripts more robust. For example, I have one bash script that
runs daily that makes an incremental backup of my hard-disk and rotates
the daily backups. If this fails, I want the script to send me an email
to let me know something went wrong. This means that every line in the
program that can possibly fail must call a "error" function that sends
an email. Here's an examples of what I have so far:
function error {
echo Failed.
ls -l $SNAPSHOT | mail seanw -s "Backup script failed"
exit
}
if [ -d $BACKUP_NAME.0 ]; then
echo Creating copy of newest backup...
rm -rf $TEMP_BACKUP || error
cp -al $BACKUP_NAME.0 $TEMP_BACKUP || error
echo Updating newest backup copy with rsync...
else
echo No previous backups found. Creating first full backup...
fi
rsync -av --delete-after --delete-excluded \
--exclude=$EXCLUDES $TO_BACKUP $TEMP_BACKUP || error
# snipped the rest...
This works fine, but I need to remember to add the "|| error" snippet
to the end of every command, it looks messy, clutters my code and I
might forget it to do it everywhere. Is there any better way to do
error handling in bash? For example, can I define a function that will
be called when the script fails or use C++ style exceptions? I noticed
that when running Makefiles, make will immediately exit if one of the
commands fail. That's similar to what I'm after, but I need to call a
function on failure.
Newsgroups: comp.os.linux.misc
From: Stephane CHAZELAS <this.addr...@is.invalid>
Subject: Re: Error handling in bash scripts
References: <1107304535.119267.126770@c13g2000cwb.googlegroups.com> <36amlfF507vhuU1@individual.net>
Mail-Copies-To: nobody
Message-ID: <slrnd01568.4jc.stephane.chazelas@spam.is.invalid>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15
Content-Transfer-Encoding: 8bit
User-Agent: slrn/0.9.8.1 (Debian)
Date: Wed, 2 Feb 2005 08:52:24 +0000
Lines: 10
Organization: Guest of ProXad - France
NNTP-Posting-Date: 02 Feb 2005 09:54:12 MET
NNTP-Posting-Host: 82.41.211.153
X-Trace: 1107334452 news10-e.free.fr 14163 82.41.211.153:32798
X-Complaints-To: abuse@proxad.net
2005-02-2, 00:54(+00), Chris F.A. Johnson:
[...]
> Would "trap error DEBUG" do what you want?
You were probably thinking of:
trap error ERR
--
Stéphane
More Articles by Tony Lawrence
Have you tried Searching this site?
Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates
This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.
Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.
Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.
We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.
Click here to add your comments
Don't miss responses! Subscribe to Comments by RSS or by Email
Click here to add your comments
If you want a picture to show with your comment, go get a Gravatar