| 1 | #!/bin/bash |
| 2 | # timeout.sh |
| 3 | |
| 4 | # Written by Stephane Chazelas, |
| 5 | #+ and modified by the document author. |
| 6 | |
| 7 | INTERVAL=5 # timeout interval |
| 8 | |
| 9 | timedout_read() { |
| 10 | timeout=$1 |
| 11 | varname=$2 |
| 12 | old_tty_settings=`stty -g` |
| 13 | stty -icanon min 0 time ${timeout}0 |
| 14 | eval read $varname # or just read $varname |
| 15 | stty "$old_tty_settings" |
| 16 | # See man page for "stty." |
| 17 | } |
| 18 | |
| 19 | echo; echo -n "What's your name? Quick! " |
| 20 | timedout_read $INTERVAL your_name |
| 21 | |
| 22 | # This may not work on every terminal type. |
| 23 | # The maximum timeout depends on the terminal. |
| 24 | #+ (it is often 25.5 seconds). |
| 25 | |
| 26 | echo |
| 27 | |
| 28 | if [ ! -z "$your_name" ] # If name input before timeout ... |
| 29 | then |
| 30 | echo "Your name is $your_name." |
| 31 | else |
| 32 | echo "Timed out." |
| 33 | fi |
| 34 | |
| 35 | echo |
| 36 | |
| 37 | # The behavior of this script differs somewhat from "timed-input.sh." |
| 38 | # At each keystroke, the counter resets. |
| 39 | |
| 40 | exit 0 |