0

I've seen this a bunch of times in bash programs. I need to start a timer that can be cancelled on key press. If no key is pressed it will execute a command. Using it for automated vagrant config.

What I want to see in the terminal:

Automatically installing in (10) ## this number should count down
Press any key to cancel automatic install.
StephenKing
  • 36,187
  • 11
  • 83
  • 112
Grant Eagon
  • 1,400
  • 1
  • 12
  • 24
  • Updating the count in-place is the trickiest part of doing this (assuming you don't want to just clear the screen each update). Other than that this is likely just a `read` with a timeout in a loop. – Etan Reisner Nov 09 '15 at 17:40

1 Answers1

3

You can use read for that with time-out option:

read -n 1 -t 10

This will wait 10 seconds for single char. If key was pressed return value will be 0:

if [ $? == 0 ]; then
    # key was pressed
    exit
fi
madneon
  • 537
  • 3
  • 17