使用read
與-t
(超時)鍵。
-t timeout
Cause read to time out and return failure if a complete
line of input is not read within timeout seconds. time‐
out may be a decimal number with a fractional portion
following the decimal point. This option is only effec‐
tive if read is reading input from a terminal, pipe, or
other special file; it has no effect when reading from
regular files. If timeout is 0, read returns success if
input is available on the specified file descriptor,
failure otherwise. The exit status is greater than 128
if the timeout is exceeded.
所以,你的情況:
read -t 30 answer
[ "$?" > 128 ] && TIMEOUT=YES
if [ "$answer" = yes ]
then
echo User replied yes
else
if [ "$TIMEOUT" = YES ]
then
echo User did not reply
else
echo User replied something other
fi
fi
讀取輸入<的/ dev/tty的取決於你在哪裏 –