Operating System - HP-UX
1753699 Members
4924 Online
108799 Solutions
New Discussion юеВ

setting read from a list of options

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

setting read from a list of options

I am attempting to create a menu that will wait for input and I want to be able to check that input is valid before the script can continue:

here is the code to display the options:

set -A CLUST ECM SIG

echo "\n\n`${BOLD}` Please select a Cluster to configure : -\n `${ENVIROCOL}`"
read ANS

cnt=1

for CLUSID in `echo ${CLUST[*]}`
do

print "\t\t${cnt})\t${CLUSID}\n"
[[ $cnt -eq ${#CLUST[*]} ]] && print "\t\tA)\tAll Clusters\n\n\t\tX)\tExit menu\n"
(( cnt +=1 ))

done

1) ECM

2) SIG

A) All Clusters

X) Exit menu

there could be up to 20 clusters but in this example there is only two.

I now need to check that the option is valid ie

1 = ECM
2 = SIG
A = ALL
X = exit

I think I need to use

while read INP
do

if [[ ??

then not sure of the regex or is there a better way to read from the input ??

Thanks for any help
:-)






hello
3 REPLIES 3
Dennis Handly
Acclaimed Contributor
Solution

Re: setting read from a list of options

You could use the select command to put up a menu with numbers and your strings. It returns that string or an error indicator.

Or you could use a case to parse the string. Or if small, you could use if with pattern matching.

In your case you could just use:
select INP in ${CLUST[*]} A X; do
case INP in
A) do_all;;
X) do_exit;;
*) do matching for each CLUST;;
esac
done

>for CLUSID in `echo ${CLUST[*]}`

You don't need that echo:
for CLUSID in ${CLUST[*]}
James R. Ferguson
Acclaimed Contributor

Re: setting read from a list of options

Hi Chris:

I like to wrap things in a 'while true' loop like this:

#!/usr/bin/sh
while true
do
clear
tput smso
echo "Host: $(hostname). $(date "+%a %x %X %Z"). [ Cluster Selection Utility ]"
tput rmso
echo "\n>>> Enter < 0 > to Exit"
echo "\n>>> Enter < l > = ECM"
echo "\n>>> Enter < 2 > = SIG"
echo "\n>>> Enter < A > = All"
echo "\n--> \c"

read CHOICE
case ${CHOICE} in
0 ) clear
exit 0
;;
1 ) ${HOME}/thing1
;;
2 ) ${HOME}/thing2
;;
A ) ${HOME}/all
;;
"") continue
;;
* )
echo "\nInvalid selection; press return to continue \c"
read BITBUCKET
;;
esac
done
...

To exit the loop and continue the encapsulating script, a 'break' statement. Otherwise after running a selected event, simply 'exit' to end the script.

Regards!

...JRF...
lawrenzo_1
Super Advisor

Re: setting read from a list of options

thanks guys,

Chris
hello