Bash traps - while emitting ERR trap
From: Andrew DeFaria (Andrew_at_DeFaria.com)
Date: 07/30/03
- Next message: Alexis Huxley: "Re: process control"
- Previous message: Leiji Liu: "test"
- Next in thread: Michael Wang: "Re: Bash traps - while emitting ERR trap"
- Reply: Michael Wang: "Re: Bash traps - while emitting ERR trap"
- Reply: Kevin Rodgers: "Re: Bash traps - while emitting ERR trap"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 30 Jul 2003 14:59:50 -0700
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 "Outside while loop"
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
Outside while loop
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
Outside while loop
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
Outside while loop
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 and ERR in the presence of while loops?
Please email to ADeFaria@Salira.com as I do not regularly read this
newsgroup
- Next message: Alexis Huxley: "Re: process control"
- Previous message: Leiji Liu: "test"
- Next in thread: Michael Wang: "Re: Bash traps - while emitting ERR trap"
- Reply: Michael Wang: "Re: Bash traps - while emitting ERR trap"
- Reply: Kevin Rodgers: "Re: Bash traps - while emitting ERR trap"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|