I am writing a script that waits until a bunch of directories appear before starting the service. It basically consists of an endless loop that breaks at the end or continues if any of the required directories are not found. Simplified, the algorithm itself looks like
loop_while_false() {
trap continue ERR
while true; do
printf .
sleep 1
false
break
done
trap ERR
echo
}
(I know that I could perform this particular behavior with untilor while !, but this is relevant to the question.)
The first time I run this, I get the expected output of a long series of points until I press ^ c. But if I run it again, I just get one point. If I don't hit ^ c, but redefine the loop to be finite, then the trap works several times in the new shell. But why break the trap for shell life? Even stranger (I spent extra time on this while StackExchange was updating the hardware), if you write a function this way, it won’t break:
loop_while_noread() {
trap continue ERR
while true; do
printf .
read -t1 -n1
break
done
trap ERR
echo
}
If you do not start first loop_while_falseand kill it with ^ c. Here is an example session:
$ trap -p
trap -- 'shell_session_update' EXIT
$ loop_while_noread
...q
$ loop_while_noread
...r
$ loop_while_noread
....^C
$ loop_while_noread
..q
$ trap -p
trap -- 'shell_session_update' EXIT
trap -- 'continue' ERR
$ loop_while_false
.....^C
$ trap -p
trap -- 'shell_session_update' EXIT
trap -- 'continue' ERR
$ loop_while_false
.
$ loop_while_noread
.
As if there is a strange relationship between sleepor falseand trap. Is this expected behavior?
I am using bash 3.2.57 (1) -release on OS X El Capitan.