Switch case
4 posters
Switch case
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 ?
[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 : 36
Location : Haldia
Re: Switch case
Ya I guess so too! But its incorrect behavior! Any solutions ?
Maithreyi- Posts : 76
Points : 142
Join date : 2010-03-03
Age : 36
Location : Haldia
Re: Switch case
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
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
Re: Switch case
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 ?
[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 : 36
Location : Haldia
Re: Switch case
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
[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 : 36
Location : Haldia
Re: Switch case
are u sure it is working for all alphabets...guess it works for a,A z,Z 0,9
not for other alphabets......
not for other alphabets......
akalya- Posts : 70
Points : 86
Join date : 2010-03-04
Re: Switch case
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
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
akalya- Posts : 70
Points : 86
Join date : 2010-03-04
Re: Switch case
i don know about shell, but in other languages, one case can be used to check only one value(not a range).
Re: Switch case
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 !
[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 : 36
Location : Haldia
Re: Switch case
@Christopher - shell does allow range!
Maithreyi- Posts : 76
Points : 142
Join date : 2010-03-03
Age : 36
Location : Haldia
Re: Switch case
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 : 36
Location : Haldia
Re: Switch case
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..
[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
Re: Switch case
Yup! range and case together playing havoc! No clue yet!
Maithreyi- Posts : 76
Points : 142
Join date : 2010-03-03
Age : 36
Location : Haldia
Re: Switch case
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
Re: Switch case
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???
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
Re: Switch case
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.
Re: Switch case
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 : 36
Location : Haldia
Permissions in this forum:
You cannot reply to topics in this forum
|
|