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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Case statement doesn't cope with values greater than 9? 1

Status
Not open for further replies.

milage

Programmer
Jul 13, 2001
58
US
Hi,

I have this case statement for which the options change depending on the number of subdirectories in a directory.

However, lets say there happen to be 15 subdirectories. This causes the case statement to echo "Invalid choice" if the user enters any values except or 1 and 5, similarly if there are 18 directories it echos "invalid choice" if the user enters any values except for 1 and 8 etc etc.

How can I stop this happening and get it to echo the choice for the range 1-14 or 1-18 etc?

Here's the code:
---------------------------------------------------------------

echo "Choose the client"
CLIENTS=`ls /support/patches`
I=0;
for client in $CLIENTS
do
I=`expr $I + 1`
echo "$I) $client"
done

while true
do
read ANS
echo $ANS
case $ANS in
[1-$I]) echo "Choose option $ANS
break
;;
*) echo "Invalid choice, please enter a valid choice"
;;
esac
done

-----

cheers

Rob

 
why not use:

if [ $ANS -ge 0 -a $ANS -le $I ]; then
echo "Choose option $ANS
break
else
echo "Invalid choice, please enter a valid choice"
fi

regular expressions in case are a pain and dynamic are as much a pain. If you stay with case then create the regular expression outside the case statement

e.g. range 1-5
RE="[12345]"
case ...
$RE) ...
...

p.s. why a re-choose message on success? Cheers,
ND [smile]

bigoldbulldog@hotmail.com
 
Hi,

thanks for that excellent suggestion, a nice quick a simple solution thats what I like, now why couldn't I have thought of that!

btw, i'm not actually re-choosing on success, there is a whole bunch of code that happens, I just removed it all and replace it with and echo to make the code sample smaller.

thanks again

Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top