Linux Read - Timeout after x seconds *idle*

Viewed 817

I have a (bash) script on a server that I have inherited the administration aspect of, and have recently discovered a flaw in the script that nobody has brought to my attention.

After discovering the issue, others have told me that it has been irritating them, but never told me (great...)

So, the script follows this concept

#!/bin/bash
function refreshscreen(){
  # This function refreshes a "statistics screen"
  ...
  echo "Enter command to override update"
  read -t 10 variable
}

This script refreshes a statistics screen, and allows the user to stall the update in lieu of commands built into a case statement. However, the read times-out (read -t 10) after 10 seconds, regardless of if the user is typing.

Long story short, is there a way to prevent read from timing out if the user is actively typing a command? Best case scenario would be a "Time out of SEC idle/inactive seconds" opposed to just timeout after x seconds.

I have thought about running a background script at the end of the cycle before the read command pauses the screen to check for inactivity, but have not found a way to make that command work.

2 Answers

I have searched for a simple solution that will do the following:

  • timeout after 10 seconds, if there is no user input at all
  • the user has infinite time to finish his answer if the first character was typed within the first 10 sec.

This can be implemented in two lines as follows:

read -N 1 -t 10 -p "What is your name? > " a
[ "$a" != "" ] && read b && echo "Your name is $a$b" || echo "(timeout)"

In case the user waits 10 sec before he enters the first character, the output will be:

What is your name? > (timeout)

If the user types the first character within 10 sec, he has unlimited time to finish this task. The output will look like follows:

What is your name? > Oliver
Your name is Oliver

Caveat: the first character is not editable, once it was typed, while all other characters can be edited (backspace and re-type). Any ideas for a simple solution?

Related