Operating System - Linux
1752770 Members
5265 Online
108789 Solutions
New Discussion юеВ

Sample Shell Script with Menu

 
Mark Treen_1
Advisor

Sample Shell Script with Menu

Hi to All

Ok maybe I am going to be chastised to not doing more research but it never hurts to ask I guess.

I need to make a basic script which when run gives the user a list say of ten things whereby they can select a number and then it performs that operation.

Does anybody know where I could find a template script to kick me off. Say something silly with a few options.

Thanks so much in advance!

Cheers

Mark
Mark Treen
9 REPLIES 9
Pete Randall
Outstanding Contributor

Re: Sample Shell Script with Menu

Mark,

See attached.


Pete

Pete
James R. Ferguson
Acclaimed Contributor

Re: Sample Shell Script with Menu

Hi Mark:

In lieu of declaring '/usr/bin/sh' in the user's initial program field in '/etc/passwd' substitute your shell (menu) script. Then make the script look something like:


#!/usr/bin/sh

PATH=/usr/sbin:/usr/bin:/sbin
trap '' INT QUIT

while true
do
clear
echo "Host: $(hostname). $(date "+%a %x %X %Z"). [ Operations Utilities ]"
echo "\n >>> Enter < 0 > to Logout"
echo "\n >>> Enter < l > to ..."
echo "\n >>> Enter < 2 > to ..."
echo "\n*---> \c"

read CHOICE
case ${CHOICE} in
0 ) exit 0
;;
1 ) ${HOME}/script/abc
;;
2 ) ${HOME}/script/xyz
;;
"") continue
;;
esac
done

exit

Regards!

...JRF...
Oviwan
Honored Contributor

Re: Sample Shell Script with Menu

Hey!

I have had a similar question, have a look at this thread, there are a lot of sample scripts
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=984784

Regards
Geoff Wild
Honored Contributor

Re: Sample Shell Script with Menu

Here's one I have used.

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.
Sandman!
Honored Contributor

Re: Sample Shell Script with Menu

Mark,

Attached is a template for a menu driven shell script. It can be used as a framework for building upon. Enjoy and hope it helps!!!

:)
Lolupee
Regular Advisor

Re: Sample Shell Script with Menu

I used this in one of my scripts. I hope it helps

#! /usr/bin/ksh
#
#
clear
writeyx () {
tput cup $2 $3
echo "$1"

}

Starting_messages () {
clear
writeyx "Please, give as many input as posible." 8 20
writeyx "USERNAME=" 10 20
writeyx "EVENTNAME=" 12 20
writeyx "SYSCALL=" 14 20
writeyx "FROM mmddhhmm[yy]" 16 20
writeyx "TO mmddhhmm[yy]=" 18 20
tput smso; writeyx "" 40 0
read user; writeyx "$user" 10 40
tput smso; writeyx "" 40 0
read event; writeyx "$event" 12 40
tput smso; writeyx "" 40 0
read syscall; writeyx "$syscall" 14 40
tput smso; writeyx "" 40 0
read from_date; writeyx "$from_date" 16 40
tput smso; writeyx "" 40 0
read to_date; writeyx "$to_date" 18 40
tput rmso

if [[ $user != "" ]]; then
OPTIONS="-u $user"
fi
if [[ $event != '' ]]; then
OPTIONS="$OPTIONS -e $event "
fi
if [[ $syscall != "" ]]; then
OPTIONS="$OPTIONS -c $syscall"
fi
if [[ $from_date != '' ]]; then
OPTIONS="$OPTIONS -t $from_date "
fi
if [[ $to_date != '' ]]; then
OPTIONS="$OPTIONS -s $to_date "
fi
tput cup 41 6
print -n "Requesting to know if $user ran command that called $syscall btw $FD to $TD"
print -n "Press y/n/q"

read ans
}

from_date () {
dates=$from_date
}
David Bellamy
Respected Contributor

Re: Sample Shell Script with Menu

/usr/bin/perl
use strict;
use diagnostics;
S_="MENU";
write;
print "Enter Choice:";
my $choice=;

if($choice=~/[Aa]) {
print "You picked option A\n";
}elsif{$choice=~/[Ll} {
exit(0);
}

format MENU
============================================
a. do something
b. do something else
l. exit
===========================================
.


Hope this helps
OMGPLSHELP
New Member

Re: Sample Shell Script with Menu

To piggy back off of Mark's question, how do you get a menu driven script to execute a simple command, for example:


echo "\n >>> Enter for the current time"
read choice
case ${choice} in



If the user selects
, how would you write this in the script to execute the time command?

Thank all!