[NTLUG:Discuss] Prompt / Response / Timeout in a script

Patrick R. Michaud pmichaud at pobox.com
Wed Sep 15 14:01:36 CDT 2004


On Wed, Sep 15, 2004 at 12:54:28PM -0500, Jack Snodgrass wrote:
> 
> 
> I need a simple ( not perl or any other add-on language ) way to prompt
> a user, take their input or continue on if a timeout period is reached.
> I want to do something like:
> 
> Select 1, 2, or 3: [1]

How about:


   #!/bin/bash

   VAL=1
   echo -n "Select 1, 2, or 3: [$VAL] ";
   read -t 5 VAL
   echo "You selected $VAL"

which issues the prompt, then waits up to 5 seconds for the user to
enter a value which will be stored in $VAL.  The argument to -t specifies
the number of seconds to wait.  If the user doesn't enter a value, then
VAL is left unchanged (i.e., to what it was at the default).

Pm
--------------
Patrick R. Michaud, Ph.D., RHCE #808002519807115
Web:   http://www.pmichaud.com
Email: pmichaud at pobox.com



More information about the Discuss mailing list