Handle a signal: Difference between revisions

Content added Content deleted
Line 1,020: Line 1,020:
^CRun for 2 seconds
^CRun for 2 seconds
</pre>
</pre>

{{works with|bash}}

Here is a version of the above which assumes that there is a controlling tty device. It exploits the POSIX standard timeout feature of the tty line discipline. Instead of executing a sleep operation, we execute a terminal read with a 5 tenths of a second timeout:

<lang bash>#!/bin/bash
trap 'echo "Run for $((s/2)) seconds"; exit' 2
s=1

half_sec_sleep()
{
local save_tty=$(stty -g)
stty -icanon time 5 min 0
read
stty $save_tty
}


while true
do
echo $s
half_sec_sleep
let s++
done</lang>


{{works with|zsh}}
{{works with|zsh}}