Blog is moving

My blog is moving to If you are looking for a specific or older post you are in the right place Otherwise check out my new page for more up to date content.

Wednesday, October 19, 2016

How to Create A Prompt With Timeout in Bash

Here's a quick function that will display a prompt with timeout in a bash script:
_myCountdownFunction () {
echo -e "Hit \"Ctrl+c\" to quit or \"Enter\" to continue...    \c" 
while (( cnt >= 0 )) ; do
  if (( cnt < 9 )) ; then
    echo -e "\b\b${cnt}s\c"
  elif (( cnt == 9 )) ; then
    echo -e "\b\b\b ${cnt}s\c"
  elif (( cnt <= 99 )) ; then
    echo -e "\b\b\b\b ${cnt}s\c"
  elif (( cnt < 999 )) ; then
    echo -e "\b\b\b\b${cnt}s\c"
  read -t 1 my_reply
  (( $? == 0 )) && exit 1
  let cnt-=1

The user will see the following message on his terminal with the seconds counting down in place:
Hit "Ctrl+c" to quit or "Enter" to continue... 5s

At the end of the specified time in 'cnt', the script (where the function would be) will continue executing, and if the user hits "Ctrl+c" before that, it will exit (both script and function).
The function supports up to 999s (which should be enough).

No comments: