Operating System - HP-UX
1833788 Members
2664 Online
110063 Solutions
New Discussion

Re: Traps in scripts with functions..

 
SOLVED
Go to solution
David Snider
Frequent Advisor

Traps in scripts with functions..

I am writing a script to kick of some system tests.. I have used many functions in my script to make it a little more manageable, the script has a lot of setup and clean up both setting up and cleaning up can take a few minutes per function, so in order to make sure it cleans up correctly when killed I have used the trap command to catch kill signals and clean up properly.. However, when sending CTRL+C, the trap command fires and starts finishing the test and cleans up, but the rest of the script continues running.. shouldn't the entire script stop except for the trap? I've verified this by putting a number of Echo statements throughout, and sure enough, when I press CTRL+C, I see echo message during the clean-up and during set-up. I want the setup portion to completely stop with CTRL+C, I suspect this has something to do with the fact that I am using functions, but am unclear of the relationship.. Should I put the trap command at the beginning of each function?
5 REPLIES 5
harry d brown jr
Honored Contributor

Re: Traps in scripts with functions..

Are you using ksh?

From the man pages of ksh:

If sig is 0 or EXIT and the trap statement is executed inside the body of a function, the command arg is executed after the function completes. If sig is 0 or EXIT for a trap set outside any function, the command arg is executed on exit from the shell.
Live Free or Die
James R. Ferguson
Acclaimed Contributor
Solution

Re: Traps in scripts with functions..

Hi David:

There is nothing wrong with placing the trap "globally" at the beginning of the script. Add an 'exit' to the commands to be executed when the trap is caught, as:

#!/usr/bin/sh
trap 'echo "bye!";exit 1' INT
function DO_IT
{
echo "waiting IO..."
read X
echo $X
}
DO_IT
exit 0
#.end.

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: Traps in scripts with functions..

Hi David,

Actually a trap can be set anywhere even within a function and its effect is global even after the function has finished. One the trap has executed, program execution resumes unless you put an exit within your trap.

Try this exeample:

#!/usr/bin/sh

trap 'echo Trap!' 2 3 15

count()
{
STOP=$1
I=1
while [ ${I} -le ${STOP} ]
do
echo "${I}"
I=`expr ${I} + 1`
done
return 0
} # count

count 100


If your send an intr via Ctrl-C the "Trap!" message will be echoed and execution will resume.

However if we modify the trap statement to:
trap 'echo Trap!; exit 1' 2 3 15

then execution stops after the echo. This should fix you.

Clay

If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: Traps in scripts with functions..

Hi David:

One more offering. Unfortunately the shell has a very limited concept of global and local environments. With traps, the last setting persists even outside the function:

#!/usr/bin/sh
trap 'echo "bye!";exit 1' INT HUP
function DO_IT
{
echo "waiting IO...but you can't interrupt me..."
read X
echo $X
}
function DO_IT_TOO
{
trap '' INT HUP
echo "waiting IO...but you can't interrupt me!!!"
read X
echo $X
}
DO_IT_TOO
DO_IT
echo "...try to interrupt now (you can't)"
read X
exit 0
#.end.

Regards!

...JRF...
David Snider
Frequent Advisor

Re: Traps in scripts with functions..

Much thanks to James and Clay.. 10 points each as it appears you were both replying at the same time!.. fledgling admin forgot the exit..


David..