Wednesday, September 2, 2009

How to prompt user to enter input within the time limit in a shell script

There are various ways to read input within the time limit in shell script. I am posting 4 methods here.First three are direct methods. Final one implemented with some logic, just for practice only.

Method 1:


#!/bin/bash
# timedinput1.sh: prompts times out at five seconds.
# Using read command

timelimit=5
echo -e " You have $timelimit seconds\n Enter your name quickly: \c"
name=""
read -t $timelimit name
#read -t $timelimit name <&1
# for bash versions bellow 3.x
if [ ! -z "$name" ]
then
echo -e "\n Your name is $name"
else
echo -e "\n TIME OUT\n You failed to enter your name"
fi
Output:
[root@localhost shell]# sh timedinput1.sh
You have 5 seconds
Enter your name quickly: king

Your name is king
[root@localhost shell]# sh timedinput1.sh
You have 5 seconds
Enter your name quickly:
TIME OUT
You failed to enter your name

Method 2:


#!/bin/bash
# timedinput2.sh
# Using stty command

timelimit=5
# Time limit to enter input
echo -e " You have only $timelimit seconds\n Enter your name quickly: \c"
name=""
stty -icanon min 0 time ${timelimit}0

# "min N" with -icanon, set N characters minimum for a completed read
# "time N" with -icanon, set read timeout of N tenths of a second (i.e. 50 means 5
seconds )

read name
if [ ! -z "$name" ]
then
echo " Your name is $name"
else
echo -e "\n TIME OUT\n You failed to enter your name"
fi
stty sane
#restore terminal settings
Output:
[root@localhost shell]# sh timedinput2.sh
You have only 5 seconds
Enter your name quickly: Sachin Ramesh Tendulkar
Your name is "Sachin Ramesh Tendulkar"
[root@localhost shell]# sh timedinput2.sh
You have only 5 seconds
Enter your name quickly:
TIME OUT
You failed to enter your name
Observation:
There is difference between method1 and method2.In method1 you should to enter input within 5 seconds.But in method 2 you have 5 seconds after a character has been hit.This is because time n means wait till n seconds after a character has been hit.So in method2 you can give any length of input.

Method3:


#!/bin/bash
# timedinput3.sh
# using TMOUT environment variable

TMOUT=5
# TMOUT is an Internal Variable
# If the $TMOUT environment variable is set to a non zero value time, then the shell prompt will time out after $time seconds.This will cause a logout.
# If you run this script in current shell after 5 seconds you will be logout

echo -e " You only have $TMOUT seconds\n Enter your name quickly: \c"
name=""
read name
if [ ! -z "$name" ]
then
echo " Your name is $name"
else
echo -e "\n TIME OUT\n You failed to enter your name"
fi
Output:
[root@localhost shell]# sh timedinput3.sh
You only have 5 seconds
Enter your name quickly: Ricky ponting
Your name is "Ricky ponting"
[root@localhost shell]# sh timedinput3.sh
You only have 5 seconds
Enter your name quickly:
TIME OUT
You failed to enter your name

Method4:


#!/bin/bash
# timedinput4.sh
# Using sleep command

timelimit=5
#set another value if you require

trap 'echo -e "\n TIMEOUT"; exit 14' 14
# Trapping signal 14

echo -e " You only have $timelimit seconds \n What is your name:\c"

sleep $timelimit && kill -s 14 $$ &

# Waits 5 seconds, then sends sigalarm to script($$ environment variable gives pid of current script).
read name
echo " Your name is \"$name\""
kill $!
#kills back ground job (i.e. sleep command)
exit
Output:
[root@localhost shell]# sh timedinput4.sh
You only have 5 seconds
What is your name:Ganguly
Your name is "Ganguly"
[root@localhost shell]# sh timedinput4.sh
You only have 5 seconds
What is your name:Kapil Dev
Your name is "Kapil Dev"
timedinput4.sh: line 16: 3814 Terminated sleep $timelimit && kill -s 14 $$
[root@localhost shell]# sh timedinput4.sh
You only have 5 seconds
What is your name:
TIMEOUT

8 comments: