I've the following 5 second timer which prints an asterisk for each second.
timer () {
i=1
trap 'i=5' INT
while [[ $i -le 5 ]]; do
sleep 1
printf "*"
((i+=1))
done
}
Somehow the trap chunk seems a little hackish and I wonder if there's a more correct way to interrupt the entire loop (not just the current sleep cycle). I've tried:
trap 'print "foo"' INT
at various locations inside and outside the function, but as I alluded to before, that just interrupts the current sleep cycle.
This works for me in bash:
and this in ksh (note the different location of the trap statement):
Perhaps I'm missing something, but what's wrong with: