2001-07-10 14:25:50 +00:00
|
|
|
#!/bin/bash
|
|
|
|
# timeout.sh
|
|
|
|
|
2004-11-15 13:35:56 +00:00
|
|
|
# Written by Stephane Chazelas,
|
|
|
|
#+ and modified by the document author.
|
2001-07-10 14:25:50 +00:00
|
|
|
|
2001-09-04 13:27:31 +00:00
|
|
|
INTERVAL=5 # timeout interval
|
2001-07-10 14:25:50 +00:00
|
|
|
|
|
|
|
timedout_read() {
|
|
|
|
timeout=$1
|
|
|
|
varname=$2
|
|
|
|
old_tty_settings=`stty -g`
|
|
|
|
stty -icanon min 0 time ${timeout}0
|
2004-11-15 13:35:56 +00:00
|
|
|
eval read $varname # or just read $varname
|
2001-07-10 14:25:50 +00:00
|
|
|
stty "$old_tty_settings"
|
2008-11-23 22:43:47 +00:00
|
|
|
# See man page for "stty."
|
2001-07-10 14:25:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
echo; echo -n "What's your name? Quick! "
|
|
|
|
timedout_read $INTERVAL your_name
|
|
|
|
|
2004-11-15 13:35:56 +00:00
|
|
|
# This may not work on every terminal type.
|
|
|
|
# The maximum timeout depends on the terminal.
|
|
|
|
#+ (it is often 25.5 seconds).
|
2001-07-10 14:25:50 +00:00
|
|
|
|
|
|
|
echo
|
|
|
|
|
2008-11-23 22:43:47 +00:00
|
|
|
if [ ! -z "$your_name" ] # If name input before timeout ...
|
2001-07-10 14:25:50 +00:00
|
|
|
then
|
|
|
|
echo "Your name is $your_name."
|
|
|
|
else
|
|
|
|
echo "Timed out."
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo
|
|
|
|
|
2008-11-23 22:43:47 +00:00
|
|
|
# The behavior of this script differs somewhat from "timed-input.sh."
|
2001-07-10 14:25:50 +00:00
|
|
|
# At each keystroke, the counter resets.
|
|
|
|
|
|
|
|
exit 0
|