Wednesday, September 12, 2012
10:00 PM

Bash Script: Set the timeout for reading the user input

If TMOUT set to a value greater than zero, TMOUT is treated as the default timeout for the read builtin.  The select command terminates if  input does  not arrive after TMOUT seconds when input is coming from a terminal. 

In an interactive shell, the value is interpreted as the number of seconds to wait for input after issuing the primary prompt.  Bash terminates after waiting for that number of seconds if input does  not arrive.

Below is the bash script which explains the above concept ...
Feel free to copy and use this script

Source: cat timeout.sh
#!/bin/bash

TMOUT=5
echo -n "Enter you username: "
read username

if [[ -z $username ]]; then
        # No username provide
        username="Not Set"
        echo "Timeout, Run this script again and set your valid username..."
fi


Output: ./timeout.sh
Enter you username: Timeout, Run this script again and set your valid username...

0 comments:

Post a Comment