APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed
RSS Feeds RSS Feeds











(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version
->
-> Trapping errors in Bash


Trapping errors in Bash


2013/08/19






I want to begin with a warning. The bash shell is really quite powerful and it is possible to do some amazingly difficult scripting using nothing more.

If you have any doubt of that, reading Chris F. A. Johnson's Shell Scripting Recipes: A Problem-Solution Approach would convince you. If Chris can write crossword puzzles using shell scripts, there's probably not much that can't be done.

That doesn't mean that you should. Complex shell scripts can be very hard to understand, impossible to modify by anyone but a true expert and are not necessarily transportable. Those statements can be true for any computer language, but shell scripts often cannot be written clearly when they reach a certain level of complexity.

The concepts demonstrated here don't reach that level, but I suspect that you'll see that they easily could. If you want to dive in and push these ideas to the limit, you can certainly do so. Just don't ask me to debug anything like that!

With that in mind, let's trap some errors.

A simple trap

This is as simple as it gets:

#!/bin/bash
trap 'echo Sorry, there was an error at line $LINENO' ERR
ls /etc/no_such_place_or_file 2>/dev/null
echo "foo"
ls /etc/no_such_place_or_file 2>/dev/null
 

Upon invocation that will spit out:

Sorry, there was an error at line 3
foo
Sorry, there was an error at line 5
 

(Unless you have a file or directory named "/etc/no_such_place_or_file", of course)

That's helpful. There is a slight problem, though:

#!/bin/bash
trap 'echo Sorry, there was an error at line $LINENO' ERR
for x in 1 2 3 4 5
do
ls /$x/nosuchplace 2>/dev/null
ls /$x/someotherplace 2>/dev/null
done
 

That always says the line number is the top of the loop

Sorry, there was an error at line 3
Sorry, there was an error at line 3
Sorry, there was an error at line 3
...
 

That's because any control structure (if, while, for, until, case) is seen as one multi-line command. There's no point to that example other than to show that.

We can do something like:

trap 'echo Sorry, there was an error at line $LINENO with $x' ERR
 

and get:

Sorry, there was an error at line 3 with 1
Sorry, there was an error at line 3 with 1
Sorry, there was an error at line 3 with 2
Sorry, there was an error at line 3 with 2
..
 

Which is some help, but control structures will obfuscate your errors.

We can get far more complicated:

#!/bin/bash
function my_trap()
{
[ $1 -lt 15 ] && echo bad bad line $1 error $2 with x at $x
[ $1 -ge 15 ] && echo REALLY bad at line $1 error $2
}

trap 'my_trap ${LINENO} $? ' ERR

for x in 1 2 3 4 5
do
ls /$x/nosuchplace 2> /dev/null
done
echo foo

ls /100/foo 2>/dev/null
 

Which should produce:

`
bad bad line 11 error 1 with x at 1
bad bad line 11 error 1 with x at 2
bad bad line 11 error 1 with x at 3
bad bad line 11 error 1 with x at 4
bad bad line 11 error 1 with x at 5
foo
REALLY bad at line 17 error 1
 

Or, you can reset the trap:

#!/bin/bash
function my_trap()
{
[ $1 -lt 15 ] && echo bad bad line $1 error $2 with x at $x
[ $1 -ge 15 ] && echo REALLY bad at line $1 error $2
}


trap 'my_trap ${LINENO} $? ' ERR
                     
for x in 1 2 3 4 5
do
ls /$x/nosuchplace 2> /dev/null
done
echo foo
trap 'echo Sorry, there was a somewhat important  error at line $LINENO ' ERR
ls /100/foo 2>/dev/null
 

Which yields:

bad bad line 11 error 1 with x at 1
bad bad line 11 error 1 with x at 2
bad bad line 11 error 1 with x at 3
bad bad line 11 error 1 with x at 4
bad bad line 11 error 1 with x at 5
foo
Sorry, there was a somewhat important error at line 17
 

What if you would rather bail out at the very first error?

You could add a "set -e" at the top of your script. That stops on any error, though, so a better way might be to add an "exit" in your trap function that is seen when and where you want it to be seen.

Finally, traps aren't just for errors.

trap "echo All Done" 0
trap OnExit EXIT INT TERM
 

The bash man page has extensive (and quite complex) information about "trap" and all its gotchas.




If this page was useful to you, please help others find it:  





Comments?




More Articles by - Find me on Google+



Click here to add your comments
- no registration needed!


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

Kerio Samepage


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.

Publishing your articles here

Jump to Comments



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.

I am a Kerio reseller. Articles here related to Kerio products reflect my honest opinion, but I do have an obvious interest in selling those products also.

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.

pavatar.jpg

This post tagged:

       - Basics
       - Linux
       - MacOSX
       - Unix















My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!


book graphic unix and linux troubleshooting guide



Buy Kerio from a dealer
who knows tech:
I sell and support

Kerio Connect Mail server, Control, Workspace and Operator licenses and subscription renewals



Click and enter your name and phone number to call me about Kerio® products right now (Flash required)