Operating System - HP-UX
1838135 Members
3095 Online
110124 Solutions
New Discussion

How to create a operation Menu

 
SOLVED
Go to solution
Kennethyap
Frequent Advisor

How to create a operation Menu

Dear Experts,
Any where I can find a template shell that use on creating menu window for operator using.
Example,
In the Menu window have selection 1,2,3...to select to execute the job, select to view the log....etc. Exit & finish will close section of the terminal.

Thks..
6 REPLIES 6
Marcel Boogert_1
Trusted Contributor
Solution

Re: How to create a operation Menu

Try the following script...

#!/bin/sh
# The menu script

while [ : ]
do
echo Choose One:
echo 1 Add file
echo 2 Remove file
echo 3 Edit file
echo 4 View file
echo 5 Quit

read i

if [ $? -eq 1 ]; then
exit 0
fi

case $i in
1) exit 1;;
2) exit 2;;
3) exit 3;;
4) exit 4;;
5) exit 5;;
*)
echo
echo Invalid choice.
echo Try again.
echo
;;
esac
done

If you want multiple menus, you can duplicate this script for submenus.

Greetings MB.
BONNAFOUS Jean Marc
Trusted Contributor

Re: How to create a operation Menu

Hi,

Use a script shell. If you want to be sure to exit terminal session when you exit menu, tell the script from the .profile of operators accounts with exec command:

.
.
exec Menu_Script

exec command doesn't create a child environnement, but REPLACE current session. When you exit menu you exit session.

Example of menu script:

...
affiche_menu
{
Your Menu (echo commands)
}

REP=1
while [ $REP != 0 ]
do
affiche_menu
read REP
case $REP in
1) Choix1;;
2) Choix2;;
....
0) echo "Bye Bye" ;;
done
exit 0



Rgds
JMB
Si vous ne faites jamais de bétises, c'est que vous ne faites rien de difficile. Et ça c'est une grosse bétise.
BONNAFOUS Jean Marc
Trusted Contributor

Re: How to create a operation Menu

Ouups,
Good code is:

affiche_menu
{
Your Menu (echo commands)
}

REP=1
while [ $REP != 0 ]
do
affiche_menu
read REP
case $REP in
1) Choix1;;
2) Choix2;;
....
0) echo "Bye Bye" ;;
esac
done
exit 0

Si vous ne faites jamais de bétises, c'est que vous ne faites rien de difficile. Et ça c'est une grosse bétise.
Jean-Louis Phelix
Honored Contributor

Re: How to create a operation Menu

Hi,

It's also possible to use builtin functions like "select" in posix shell (see man sh-posix). Example :

#!/usr/bin/sh
PS3="Your choice ? "
select A in MONDAY TUESDAY WEDNESDAY exit
do
[ -z "$A" ] && continue
[ "$A" = exit ] && break
echo "My prefered day is $A (choice $REPLY)"
done

Execution :

phelix> /tmp/a
1) MONDAY
2) TUESDAY
3) WEDNESDAY
4) exit
Your choice ? 2
My prefered day is TUESDAY (choice 2)
Your choice ? 0
Your choice ? 4
phelix>

Regards.
It works for me (© Bill McNAMARA ...)
Geoff Wild
Honored Contributor

Re: How to create a operation Menu

Here's one, see attached.

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Amiel Tutolo
Frequent Advisor

Re: How to create a operation Menu

Here is another one. Hope it helps...
Live, love and laugh