I need to listen for any key press in a countdown timer loop. If any key is pressed then the countdown timer should break out of it\'s loop. This mostly works except for t
The problem is that read
would by default consider a newline as a delimiter.
Set the IFS
to null to avoid reading upto the delimiter.
Say:
IFS= read -s -N 1 -t 1 key
instead and you'd get the expected behavior upon hitting the Enter key during the read
.
I think based on the return code of read
, there is a work around for this problem. From the man
page of read
,
The return code is zero, unless end-of-file is encountered, read times out,
or an invalid file descriptor is supplied as the argument to -u.
The return code for timeout seems to be 142
[verified in Fedora 16]
So, the script can be modified as,
#!/bin/bash
for (( i=30; i>0; i--)); do
printf "\rStarting script in $i seconds. Hit any key to continue."
read -s -n 1 -t 1 key
if [ $? -eq 0 ]
then
break
fi
done
echo "Resume script"