Switch case

Go down

Switch case

Post by Maithreyi on Wed Mar 10, 2010 2:20 pm

case $1 in
[a-z]) echo "You entered a small alphabet" ;;
[A-Z]) echo "you entered a capital alphabet" ;;
[0-9]) echo "You entered a digit";;
*) echo "Unknown entry" ;;
esac


This is a shell script.
Suppose I run ./sample A
it says you entered a small alphabet.
suppose I run ./sample a
it says you entered a small alphabet

Now when I interchange the first two statements in my switch case script, then
[A-Z]) echo "you entered a capital alphabet" ;;
[a-z]) echo "You entered a small alphabet" ;;


It says you entered a capital alphabet whether my input is caps or small.Why this divergence ?

Any explanation ?

Maithreyi

Posts : 76
Points : 142
Join date : 2010-03-03
Age : 30
Location : Haldia

View user profile

Back to top Go down

Re: Switch case

Post by Christopher on Wed Mar 10, 2010 3:07 pm

it ignores the case i guess...
avatar
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 30

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: Switch case

Post by Maithreyi on Wed Mar 10, 2010 3:17 pm

Ya I guess so too! But its incorrect behavior! Any solutions ?

Maithreyi

Posts : 76
Points : 142
Join date : 2010-03-03
Age : 30
Location : Haldia

View user profile

Back to top Go down

Re: Switch case

Post by akalya on Wed Mar 10, 2010 3:52 pm

guess its not related case-sensitive/insensitive issues
if so how come the following works???

case ${option} in
a)echo "You entered a small alphabet:$option" ;;
A)echo "you entered a capital alphabet:$option" ;;
[0-9] )echo "You entered a digit";;
*) echo "Unknown entry" ;;
esac

[335802@oracleclient ~]$ ./whiletest A
A
option:A
you entered a capital alphabet:A
[335802@oracleclient ~]$ ./whiletest a
a
option:a
You entered a small alphabet:a

akalya

Posts : 70
Points : 86
Join date : 2010-03-04

View user profile

Back to top Go down

Re: Switch case

Post by Maithreyi on Wed Mar 10, 2010 4:45 pm

case $1 in
[a-z]) echo "You entered a small alphabet" ;;
[A-Z]) echo "you entered a capital alphabet" ;;
[0-9]) echo "You entered a digit";;
*) echo "Unknown entry" ;;
esac

[335818@oracleclient ~]$ ./sample A
You entered a small alphabet
[335818@oracleclient ~]$ ./sample Z
you entered a capital alphabet

This is the result I got.Does this mean case doesn't process patterns ?
When I gave a-s and A-S it said S is a capital alphabet but everything else was a small alphabet ? Any clue ?

Maithreyi

Posts : 76
Points : 142
Join date : 2010-03-03
Age : 30
Location : Haldia

View user profile

Back to top Go down

Re: Switch case

Post by Maithreyi on Wed Mar 10, 2010 4:57 pm

case $1 in
[a,z]) echo "You entered a small alphabet" ;;
[A,Z]) echo "you entered a capital alphabet" ;;
[0,9]) echo "You entered a digit";;
*) echo "Unknown entry" ;;
esac

This works perfectly! So the problem is when I give range

Maithreyi

Posts : 76
Points : 142
Join date : 2010-03-03
Age : 30
Location : Haldia

View user profile

Back to top Go down

Re: Switch case

Post by akalya on Wed Mar 10, 2010 5:05 pm

are u sure it is working for all alphabets...guess it works for a,A z,Z 0,9
not for other alphabets......

akalya

Posts : 70
Points : 86
Join date : 2010-03-04

View user profile

Back to top Go down

Re: Switch case

Post by akalya on Wed Mar 10, 2010 5:40 pm

and i don know if the problem is with range bcoz the following works...

echo "enter option:"
read a
case $a in
[A-Z]) echo "you entered a capital alphabet:$a" ;;
[0-9]) echo "You entered a digit" ;;
?) echo "special symbol" ;;
*) echo "Unknown entry" ;;
esac

[335802@oracleclient ~]$ sh make
enter option:
a
special symbol
[335802@oracleclient ~]$ sh make
enter option:
F
you entered a capital alphabet:F


check out Smile

akalya

Posts : 70
Points : 86
Join date : 2010-03-04

View user profile

Back to top Go down

Re: Switch case

Post by Christopher on Wed Mar 10, 2010 5:44 pm

i don know about shell, but in other languages, one case can be used to check only one value(not a range).
avatar
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 30

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: Switch case

Post by Maithreyi on Wed Mar 10, 2010 5:49 pm

case $1 in
[A-Z]) echo "you entered a capital alphabet" ;;
[0-9]) echo "You entered a digit";;
*) echo "Unknown entry" ;;
esac

Here capital letter works fine! Because there is no interference with small letter.But if i give ./sample a it still gives capital letter

Anyway the shell is confused !

Maithreyi

Posts : 76
Points : 142
Join date : 2010-03-03
Age : 30
Location : Haldia

View user profile

Back to top Go down

Re: Switch case

Post by Maithreyi on Wed Mar 10, 2010 5:50 pm

@Christopher - shell does allow range!

Maithreyi

Posts : 76
Points : 142
Join date : 2010-03-03
Age : 30
Location : Haldia

View user profile

Back to top Go down

Re: Switch case

Post by Christopher on Wed Mar 10, 2010 6:11 pm

are u sure tat case supports range?? i don think so.
avatar
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 30

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: Switch case

Post by Maithreyi on Wed Mar 10, 2010 6:47 pm

If ranges didn't work 0-9 should have failed as well.But, thats working perfectly!

Maithreyi

Posts : 76
Points : 142
Join date : 2010-03-03
Age : 30
Location : Haldia

View user profile

Back to top Go down

Re: Switch case

Post by Vineet_More on Wed Mar 10, 2010 6:55 pm

case $1 in
[A-I]) echo \"you entered a capital alphabet\" ;;
[0-9]) echo \"You entered a digit\";;
*) echo \"Unknown entry\" ;;
esac
tried this command got error on the following inputs:
e,d,f,g,h,c,b,i

I think there might be other reason for the command not to work properly apart from the range..


Last edited by Vineet_More on Wed Mar 10, 2010 6:57 pm; edited 1 time in total

Vineet_More

Posts : 27
Points : 31
Join date : 2010-02-26

View user profile

Back to top Go down

Re: Switch case

Post by Maithreyi on Wed Mar 10, 2010 6:56 pm

Yup! range and case together playing havoc! No clue yet!

Maithreyi

Posts : 76
Points : 142
Join date : 2010-03-03
Age : 30
Location : Haldia

View user profile

Back to top Go down

Re: Switch case

Post by Christopher on Wed Mar 10, 2010 7:13 pm

try this code

Code:
case $1 in
[abcdefghijklmnopqrstuvwxyz]) echo "You entered a small alphabet";;
[ABCDEFGHIJKLMNOPQRSTUVWXYZ]) echo "you entered a capital alphabet";;
[0-9]) echo "You entered a digit";;
*) echo "Unknown entry";;
esac
hope it works well. but i didn't test for all possible inputs.
avatar
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 30

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: Switch case

Post by Christopher on Wed Mar 10, 2010 7:14 pm

but why it s not working when we specify range??? confused
avatar
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 30

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: Switch case

Post by Vineet_More on Wed Mar 10, 2010 7:28 pm

I think case doesnt work in case statement. when we are specifying a range
but if we specify a range of [A-I] let say it works for both A-I and b-i except samll a. why???

Vineet_More

Posts : 27
Points : 31
Join date : 2010-02-26

View user profile

Back to top Go down

Re: Switch case

Post by Christopher on Wed Mar 10, 2010 9:40 pm

im using ubuntu 9.10 in my laptop. when i try to run tat pgm(case with range), it works well. i think it is something to do with shell version. scratch scratch
avatar
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 30

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: Switch case

Post by Maithreyi on Thu Mar 11, 2010 11:03 am

Yes it has something to do with the version of the shell! By logic, there is no flaw and it should work.

Maithreyi

Posts : 76
Points : 142
Join date : 2010-03-03
Age : 30
Location : Haldia

View user profile

Back to top Go down

Re: Switch case

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum