Shell Script taking Time bound Input !!

Here is a simple shell script that will take the input from user keyboard and displays it on the screen. But the idea is not that!!!

Idea is to show you, how to take “time bound” input for sensitive information.

vim timed_read.sh

and here is the script…

#!/bin/bash
#alok srivastava
#taking time bound input

TIMEOUTLIMIT=4  # 4 seconds of wait

echo “type something ”
read -t $TIMEOUTLIMIT first <&1
echo

if [ -z “$first” ]
then
echo “timed out.. sorry”
else
echo “you typed $first”
fi

exit 0   # exit clean

When you execute this script using “sh time_bound.sh” and give some input within the time frame of 4 seconds it will be displayed.. else the script will exit cleanly after waiting for 4 seconds. As shown:

time bound script output

Output of above Script

God Bless.

Advertisements