Alright gang, I've done pretty exhaustive search on effective uses of the 'trap' command - and have found that everybody out there uses the same example...
trap "rm -f $tmp; exit" 0 1 2 15
...and that doesn't do a whole lot for me.
I'm looking to ignore all 1's and trap, notify, and exit 1 on anything else - OR the 2nd instance of a 1 signal.
I've tried:
trap '' 1
trap TRAPPER ERR
(where TRAPPER is a sub-routine that sends a message then exits)
but the script keeps looping eventualy overunning my buffer and giving me all kinds of grief - so any suggestions? Then goal in all of this is to come up with a *really* good error handler for unix scripts that functions similar to:
VBA's "on ERR goto ERROR_HANDLER" and
SQL's "whenever sqlerror exit 1"
statements.
thanks,
Ursa Major
mailto:bear.barrow@echostar.com
First, the ERR trap seems to work in a straight forward manner. I tried:
#! /usr/bin/ksh
TRAPPER() { echo trapper ran ; }
trap TRAPPER ERR
false
true
false
exit 0
And I got pretty much what I expected. This is the first time I have ever trapped on ERR though. So it's not like I have a lot of experience with it.
The second part of your question has raised an interesting point. Here is a script that exits after it gets two signal 15's...
#! /usr/bin/ksh
c=0
TRAPPER() { echo TRAPPER ran ; ((c=c+1)) ; ((c==2)) && exit 0 ; }
trap TRAPPER 15
echo my pid is $$
while : do
sleep 5
echo still here
done
exit 0
And again this script works just as I would expect. From another window, I just send a couple of signal 15 to it and it dies on the second one. But when I try this with signal 1, I fail! And I'm not sure why. If looks like ksh is treating signal 1 specially. I'm not sure why this is. It may be a bug.
Cool - thanks. One quick question - exactly how is it that you 'send' the 15 signal to the executing script?
I send the signal via kill:
kill -15 pid
where pid is the value displayed by the script in the other window.
:D, duh! Thanks!
I've also been given a number of other options which I'm going to munge together and see what happens - I'm betting I'll have more questions - this 'trap' doesn't seem all that friendly/useful if you ask me...(which you didn't, but...
Off to unixland...