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

Help with a case statement? 1

Status
Not open for further replies.

eli101

IS-IT--Management
Feb 1, 2001
54
US
I trying to make a case statement like this, but this dose not work
----------------------------------------------
echo "Please Enter"
read ANS
case $ANS in
${ANS} –gt ) echo $ANS;;
${ANS} –lt 7) echo $ANS;;
esac

Is there a way to Wright a case that should have values as part of the case
Any help is appreciated
Thanks Eli
 
Hi Eli,

If you want to use multiple values in a case statement, you have to specify and separate them with a pipe

case $ANS in
1|2|3) do_something;;
4|5) do_something_else;;
*) do_whatever_you_want;;
esac
 
Here's a slightly different take using basic regular expressions

echo "Please Enter"
read ANS
case $ANS in
[0-6]) echo &quot;$ANS is < 7&quot;;;
[0-9]*) echo &quot;$ANS is >= 7&quot;;;
*) echo huh?;;
esac

of course entering 03 results in >= 7 so something more hairy could be put in such as
[7-9]|[1-9][0-9]*

and/or...

First you may (should) even want to trim whitespace and prefixes of zeros before using the case command. Cheers,
ND [smile]

bigoldbulldog@hotmail.com
 
Sorry I did not explained my self well

I am tying to make a case with conditions with numbers grater then 9 like 17-27

echo &quot;Please Enter&quot;
read ANS
case $ANS in
[17-27]) echo &quot;$ANS is < 7&quot;;;
[28-35]*) echo &quot;$ANS is >= 7&quot;;;
*) echo huh?;;
esac

any help?
thanks
 
echo &quot;Please Enter&quot;
read ANS
case $ANS in
[1-9][0-9]*) echo &quot;$ANS is > 9&quot;;;
*) echo huh?;;
esac

Why not use an if or test statement if you have a simple condition of success or failure. e.g.

if [ $ANS -gt 9 ]; then
echo &quot;$ANS is > 9&quot;
else
echo huh?
fi
Cheers,
ND [smile]

bigoldbulldog@hotmail.com
 
Thanks for the help,

I can not use a if statement because it will be to complex
so I was trying to use a case like
from 1 to 7
7 to 12
15 to 34
35 to 50
The problem is that with case it works only from 0 to 9 so I tried using expr but with no luck this was the way I tried

#!/bin/sh
set -x
echo &quot;Please Enter&quot;
read ANS
case &quot;expr '$ANS'&quot; in
[1-9]) echo &quot;1 to9&quot;;;
&quot;expr '26'&quot;) echo &quot;26&quot;
esac


Thanks again for your help

If any one could help me how to use it with a if statement but this is a long complex script
 
Here's a Korn shell version:

[tt]#!/bin/ksh

read ANS
if [[ &quot;$ANS&quot; != +([0-9]) ]]
then
echo &quot;it's not numeric&quot;
elif [[ &quot;$ANS&quot; -ge 0 && &quot;$ANS&quot; -le 7 ]]
then
echo &quot;it's between one and seven&quot;
elif [[ &quot;$ANS&quot; -ge 7 && &quot;$ANS&quot; -le 12 ]]
then
echo &quot;it's between seven and twelve&quot;
elif [[ &quot;$ANS&quot; -ge 15 && &quot;$ANS&quot; -le 34 ]]
then
echo &quot;it's between fifteen and thirty-four&quot;
elif [[ &quot;$ANS&quot; -ge 35 && &quot;$ANS&quot; -le 50 ]]
then
echo &quot;it's between thirty-five and fifty&quot;
fi[/tt]
Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top