Operating System - HP-UX
1827807 Members
2341 Online
109969 Solutions
New Discussion

script a timeout into a menu

 
SOLVED
Go to solution
John Meissner
Esteemed Contributor

script a timeout into a menu

I'm trying to create a menu that will put a timeout into a menu and I'm not having much luck. If the user does not interact in 10 seconds I want the script to choose a default choice for him/her. Here is the script:

function main {
tvar=10
while [ "$tvar" >= "0" ]
do
clear
echo " ${BOLD}Main Boot Menu${NORMAL}"
echo " "
echo " Please choose on of the following"
echo " "
echo " ${BOLD}1${NORMAL} Default"
echo " ${BOLD}2${NORMAL} N-1"
echo " ${BOLD}3${NORMAL} R2"
echo " "
echo " you have ${BOLD}$tvar${NORMAL} seconds to make your selection before \"default\" is used"
sleep 1
((tvar=$tvar-1))
#done

read choice
until [ "$choice" = "1" ] || [ "$choice" = "2" ] || [ "$choice" = "3" ] ;
do
echo "Please choose one of the options above"
done

case $choice in
1)
default
;;
2)
N1
;;
3)
R2
;;
esac
done
default
}

All paths lead to destiny
2 REPLIES 2
James R. Ferguson
Acclaimed Contributor
Solution

Re: script a timeout into a menu

Hi John:

Instead of a simple 'read' use 'line -t timeout':

# CHOICE=`line -t 10`

See the man pages for 'line' for more information.

Regards!

...JRF...
John Meissner
Esteemed Contributor

Re: script a timeout into a menu

James - Thanks ... that was exactly what I was looking for. I was hoping to have a countdown clock display counting down the time remaining to make a selection but I'll take what I can get...

Here is the final script:


function main {
clear
echo " ${BOLD}Main Boot Menu${NORMAL}"
echo " "
echo " Please choose on of the following"
echo " "
echo " ${BOLD}1${NORMAL} Default - test and developement"
echo " ${BOLD}2${NORMAL} Disaster Recovery N-1 copy for `hostname`"
echo " ${BOLD}3${NORMAL} Disaster Recovery R2 copy for `hostname`"
echo " "
echo "You have 10 seconds to make a selection"
print -n "Please choose one of the options above:"

choice=`line -t 10`
until [ "$choice" = "1" ] || [ "$choice" = "2" ] ||
[ "$choice" = "3" ] || [ "$choice" = "" ] ;
do
print -n "Please choose one of the options above"
choice=`line -t 10`
done

if [ "$choice" = "" ] || [ "$choice" = "1" ] ;
then
default
elif [ "$choice" = "2" ]
then
N1
elif [ "$choice" = "3" ]
then
R2
fi
}
All paths lead to destiny