This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Bash traps - while emitting ERR trap


Given the following script:

#!/bin/bash
function cleanup {    echo "ENTER cleanup"
} # cleanup
                  trap cleanup EXIT ERR
           declare -i i=4           echo "Entering while loop"
while [ $i -gt 0 ]; do
echo "In while loop"
let i=i-1
done                   echo "After trap is set"
Why is "ENTER cleanup" echoed twice when run?

$ traptest
Entering while loop
In while loop
In while loop
In while loop
In while loop
ENTER cleanup
After trap is set
ENTER cleanup

If I remove the EXIT from the trap statement then I get the following

Entering while loop
In while loop
In while loop
In while loop
In while loop
ENTER cleanup
After trap is set

and if I omit the ERR from the trap statement I get the following:

Entering while loop
In while loop
In while loop
In while loop
In while loop
After trap is set
ENTER cleanup

It seems as though the EXIT trap is getting invoked properly when the script exits but the ERR trap is being generated upon exit of the while loop. Is this correct behavior? If is it correct behavior then how can I script a cleanup routine to trap properly for such things as EXIT ERR and INT in the presence of while loops?



--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]