Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I create/find a user options menu 2

Status
Not open for further replies.

nogs

Technical User
Aug 3, 2001
89
0
0
GB
Script below isnt working correctly, had wanted it to logout from the menu if no keystrokes for 30 minutes - can any one see where ive gone worong?
Thanks for your help in advance

#!/bin/sh
ok=0
while [ $ok -eq 0 ]; do
clear
cat <<__END




MENU
------------------------
Choose an option.

(1).
(2).
(3). Email.
(4).
(5). Lynx.
(6). Admin.
(15). Logout.

`logname` `date '+Terminal LAST USED: %H:%M:%S'`


__END
COUNTER=0
while [ $COUNTER -lt 30 ];
do
sleep 60
COUNTER=`echo &quot;$COUNTER + 1&quot;|bc`
if [ $COUNTER >29 ]
then
echo &quot;you were idle too long&quot;
ok=1
exit
fi
done


read ans
case &quot;$ans&quot; in
1)
echo
;;
2)
echo
;;
3)
echo Email. ; /appl/pine/pine
;;
4)
echo Generic. ; ./gen.sh
;;
15)
exit
;;
 
Do you try this ?
if [ $COUNTER -gt 29 ]

I don't see the &quot;done&quot; of the first while. Is it normal ?
 
tof
Have now changed the script as suggested still isnt working?

It seems to be problem is caused because;
the loop wont allow user response to be registed, therefore I changed it, to have the loop at the end - this caused a problem because the script is now sat waiting for &quot;get ans&quot;?!? New script below;

#!/bin/sh
set -x

COUNTER=1
while [ &quot;$COUNTER&quot; -le &quot;30&quot; ];
do

clear

cat <<__END




MENU
------------------------
Choose an option.

(1).
(2).
(3). Email.
(4).
(5). Lynx.
(6). Admin.
(15). Logout.

`logname` `date '+Terminal LAST USED: %H:%M:%S'`


__END

read ans

case &quot;$ans&quot; in

1)

;;
2)

;;
3)
echo Email. ; /appl/pine/pine
;;
4)
echo Generic. ; ./gen.sh
;;
15)
ok=1
#exit
;;
*)
echo &quot;Invalid Option&quot;
echo &quot;Press Enter to continue&quot;
read yn
;;
esac

COUNTER=`echo &quot;$COUNTER + 1&quot;|bc`
sleep 60


done
 
Here's a menu function you can use to
atomatically generate menus for your script.
faq80-1606

I will work on a solution for your auto logout.
I think a background process may work.

Robert



Robert G. Jordan

Robert@JORDAN2000.com
Unix Sys Admin
Chicago, Illinois U.S.A.
[lightsaber]
 
To my suprise, I actually got this to work.
I didn't know until now that a funciton
could be called in the background.
I tried to use variables to set the value
for KEY_PRESS, but I couldn't get the
the background function to see the new value
I was setting. So I ended up writing the
true/false value to a temporary file.

Robert

# test background timeout function
TMP_LIST=/tmp/TMP_LIST.$$

Start_Timer ()
{
KEY_PRESS=false
echo &quot;false&quot; > $TMP_LIST.KEY_PRESS
sleep 5
KEY_PRESS=`cat $TMP_LIST.KEY_PRESS`
if [ &quot;$KEY_PRESS&quot; = &quot;false&quot; ]
then
echo
echo &quot;You're too slow!&quot;
echo &quot;Session timed out!&quot;
fi
}

Read_Input ()
{
echo &quot;Type something and press [ENTER] --> \c&quot;
read INPUT
echo &quot;true&quot; > $TMP_LIST.KEY_PRESS
echo &quot;You typed: $INPUT&quot;
}

Start_Timer & # call thisi timer function in the background
Read_Input # call this function to read input in the foreground
Robert G. Jordan

Robert@JORDAN2000.com
Unix Sys Admin
Chicago, Illinois U.S.A.
[lightsaber]
 
Robert
How would be the best way of incorporating the timer to my menu?
Thanks for all your help

Nogs
 
You just need to call the
Start_Timer function at the start of each menu.

and add this code after each answer is entered.
echo &quot;true&quot; > $TMP_LIST.KEY_PRESS
Robert G. Jordan

Robert@JORDAN2000.com
Unix Sys Admin
Chicago, Illinois U.S.A.
[lightsaber]
 
Here's a simpler way to do it.

Start_Timer ()
{
TMP_LIST=/tmp/TMP_LIST.$$
KEY_PRESS=false
echo &quot;false&quot; > $TMP_LIST.KEY_PRESS
sleep 5
KEY_PRESS=`cat $TMP_LIST.KEY_PRESS`
if [ &quot;$KEY_PRESS&quot; = &quot;false&quot; ]
then
echo
echo &quot;You're too slow!&quot;
echo &quot;Session timed out!&quot;
fi
}

Stop_Timer ()
{
echo &quot;true&quot; > $TMP_LIST.KEY_PRESS
}

Just add the two functions above to above your
program code in the script.

Here's an example:

Start_Timer
echo &quot;Enter your name: \c&quot;
read NAME
Stop_Timer Robert G. Jordan

Robert@JORDAN2000.com
Unix Sys Admin
Chicago, Illinois U.S.A.
[lightsaber]
 
Thanks for your help RobertG - I will try this later

Nogs
 
For small scripts, you could use:
Code:
#!/usr/local/bin/bash

select name in &quot;ls -al&quot; &quot;who&quot; &quot;id&quot;
do
    eval $name
done
As to how to include timeouts in this, I'm not sure...
Cheers, Neil :)
 
Robert
How do I break/exit from the menu within start_timer, I have tried this;
Start_Timer ()
{
TMP_LIST=/tmp/TMP_LIST.$$
KEY_PRESS=false
echo &quot;false&quot; > $TMP_LIST.KEY_PRESS
sleep 5
KEY_PRESS=`cat $TMP_LIST.KEY_PRESS`
if [ &quot;$KEY_PRESS&quot; = &quot;false&quot; ]
then
#echo &quot;You're too slow!&quot;
#echo &quot;Session timed out!&quot;
exit
fi
}

Stop_Timer ()
{
echo &quot;true&quot; > $TMP_LIST.KEY_PRESS
}

while true;
do

Start_Timer &
Display_Menu
Read_Answer
Stop_Timer

 
Here's the new version.
I added a kill $$ statement
to exit the script upon timeout.

!#/usr/bin/ksh
# Name: timer.fun
# Date: 03/25/2002
# Updated: 03/26/2002
# Version: 1.1
# Author: Robert G. Jordan Robert@JORDAN2000.com
# Description: Add a timeout timer to your shell scripts
# with Start_Timer and Stop_Timer functions

################################################################################
# FUNCTIONS
################################################################################

Start_Timer () # requires argument seconds, ex: Start_Timer 300 creates a 5 minute timeout
{
Background_Timer () # sub funciton to run timer in the background
{
sleep $SECONDS
TIMER=`cat $TMP_FILE.Timer` # check to see if the timer has been turned off
if [ &quot;$TIMER&quot; = &quot;on&quot; ]
then
echo
echo &quot;TIMER: $TIMER&quot;
echo &quot;Program timed out!&quot;
kill $$
fi
}
SECONDS=$1
echo &quot;on&quot; > $TMP_FILE.Timer # turn timer on
Background_Timer & # call sub function in the background
}

Stop_Timer ()
{
echo &quot;off&quot; > $TMP_FILE.Timer
}

################################################################################
# VARIABLES
################################################################################

TMP_FILE=/tmp/TMP_FILE.$$

################################################################################
# MAIN
################################################################################

# TEST RUN
Start_Timer 5 # start a 5 second timer
echo &quot;Type something and press [ENTER] --> \c&quot;
read INPUT
Stop_Timer # stop the timer
echo &quot;You typed: $INPUT&quot;

################################################################################
# END
################################################################################
Robert G. Jordan

Robert@JORDAN2000.com
Unix Sys Admin
Chicago, Illinois U.S.A.
[lightsaber]
 
Robert
Seems to work ok in the &quot;test&quot; format you specified - if you add it to my menu script - it just flashes up the screen. Have tried fiddling with various bits to no avail, here is the script Im using;

#!/bin/sh
#set -x

Display_Menu ()
{
clear

cat <<__END




MENU
------------------------
Choose an option.

(1). etc.
(2). etc.
(3). etc.
(4). etc.
(5). etc.
(6). Admin.
(15). Logout.

`logname` `date '+Terminal LAST USED: %H:%M:%S'`


__END
}

Read_Answer ()
{
read ans
}

Start_Timer () # requires argument seconds, ex: Start_Timer 300 creates a 5 minute timeout
{
Background_Timer () # sub funciton to run timer in the background
{
sleep $SECONDS
TIMER=`cat $TMP_FILE.Timer` # check to see if the timer has been turned off
if [ &quot;$TIMER&quot; = &quot;on&quot; ]
then
echo
echo &quot;TIMER: $TIMER&quot;
echo &quot;Program timed out!&quot;
kill $$
fi
}
SECONDS=$1
echo &quot;on&quot; > $TMP_FILE.Timer # turn timer on
Background_Timer & # call sub function in the background
}

Stop_Timer ()
{
echo &quot;off&quot; > $TMP_FILE.Timer
}

################################################################################
# VARIABLES
################################################################################

TMP_FILE=/tmp/TMP_FILE.$$

################################################################################
# MAIN
################################################################################
while true;
do

Start_Timer 5
Display_Menu
Read_Answer
Stop_Timer

case &quot;$ans&quot; in.........etc

 
First start, by disabling certain functions
so you can isolate the problem.

Like this:

# Start_Timer 5
Display_Menu
Read_Answer
# Stop_Timer

Also, I noticed you don't have matching &quot;done&quot;
for your &quot;do&quot; statement at the end.

Robert Robert G. Jordan

Robert@JORDAN2000.com
Unix Sys Admin
Chicago, Illinois U.S.A.
[lightsaber]
 
Heres one that got Monkeyed :)
#!/bin/ksh
# Name: timer.fun
# Date: 03/25/2002
# Updated: 04/04/2002
# Version: 1.2
# Author: Robert G. Jordan Robert@JORDAN2000.com
# Monkey: K.Haughton Kev@virtualkev.com
# Description: Add a timeout timer to your shell scripts
# with Start_Timer and Stop_Timer functions
# Modded by $Monkey: this now includes a basic menu script
# to control user added commands :)
#set -x
################################################################################
# FUNCTIONS
################################################################################

Start_Timer () # requires argument seconds, ex: Start_Timer 300 creates a 5 minute timeout
{
Background_Timer () # sub funciton to run timer in the background
{
sleep $SECONDS
TIMER=`cat $TMP_FILE.Timer` # check to see if the timer has been turned off
if [ &quot;$TIMER&quot; = &quot;on&quot; ]
then
echo
echo &quot;TIMER: $TIMER&quot;
echo &quot;Program timed out!&quot;
kill $$
fi
}
SECONDS=$1
echo &quot;on&quot; > $TMP_FILE.Timer # turn timer on
Background_Timer & # call sub function in the background
}

Stop_Timer ()
{
echo &quot;off&quot; > $TMP_FILE.Timer
}

################################################################################
# VARIABLES
################################################################################

TMP_FILE=/tmp/TMP_FILE.$$

################################################################################
# MAIN
################################################################################

# TEST RUN
Start_Timer 15 # start a 5 second timer
while true
do

clear

cat <<__END




MENU
------------------------
Choose an option.

(1). .
(2). .
(3). Email.
(4). .
(5). Lynx.
(6). Admin.
(15). Logout.

`logname` `date '+Terminal LAST USED: %H:%M:%S'`


__END


read ans


case &quot;$ans&quot; in

1)
echo command. #;
Stop_Timer; command here
;;
2)
echo command. #;
Stop_Timer; command here
;;
3)
echo Email. #;
Stop_Timer; /appl/pine/pine
;;
4)
echo command. #;
Stop_Timer; command here
;;
5) echo LYNX. #;
Stop_Timer; /appl/bin/lynx
;;



15)
exit
;;
*)
echo &quot;Invalid Option&quot;
echo &quot;Press Enter to continue&quot;
read yn
;;
esac

Stop_Timer # stop the timer
done

################################################################################
# END
################################################################################ ***************************************
Party on, dudes!
 
I'm glad it worked for you.
I like your cat solution for displaying
a block of text on screen.

Robert

cat <<__END




MENU
------------------------
Choose an option.

(1). .
(2). .
(3). Email.
(4). .
(5). Lynx.
(6). Admin.
(15). Logout.

`logname` `date '+Terminal LAST USED: %H:%M:%S'`


__END
Robert G. Jordan

Robert@JORDAN2000.com
Unix Sys Admin
Chicago, Illinois U.S.A.
[lightsaber]
 
Thanks for all your help Robert

Nogs
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top