Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash script countdown timer needs to detect any key to continue

Tags:

bash

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 the enter key just makes the countdown timer go faster.

#!/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 [[ $key ]]
    then
        break
    fi
done
echo "Resume script"

I just can't seem to find any examples of how to detect that enter key anywhere online.

like image 239
Captain Jimmy Avatar asked Jan 21 '14 07:01

Captain Jimmy


2 Answers

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"
like image 186
Sakthi Kumar Avatar answered Nov 17 '22 02:11

Sakthi Kumar


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.

like image 34
devnull Avatar answered Nov 17 '22 03:11

devnull