The first argument to a bash shell script is usually assigned to $1. Are you assigning that to your $var variable elsewhere in your script? If you change $var to $1 it works fine.
Below is the full code - I cant able to find out what causes this error
echo "Enter the character"
read var
case $var in
[a-z])
echo "Entered a lowercase alphabet"
;;
[A-Z])
echo "Entered a uppercase alphabet"
;;
[0-9])
echo "Entered a numeric value"
;;
?)
echo "Entered a special character"
;;
*)
echo "Entered more than one character"
;;
esac
Put quotes around the $var variable in the case statement:
echo "Enter the character: "
read var
case "$var" in
[a-z])
echo "Entered a lowercase alphabet"
;;
[A-Z])
echo "Entered a uppercase alphabet"
;;
[0-9])
echo "Entered a numeric value"
;;
?)
echo "Entered a special character"
;;
*)
echo "Entered more than one character"
;;
esac
Unless the shell's case-statement implementation is buggy, double-quoting won't make a difference, since it only prevents expansions which won't occur anyway (field splitting and pathname expansion).
Regards,
Alister
---------- Post updated at 10:06 AM ---------- Previous update was at 10:00 AM ----------
That sounds like a locale collation issue to me. Your locale probably interleaves upper and lower case letters. Either switch to the C (aka POSIX) locale or use character classes, [[:upper:]] and [[:lower:]], which are valid in all locales.
---------- Post updated at 03:32 PM ---------- Previous update was at 03:10 PM ----------
It works longhand:-
[abcdefghijklmnopqrstuvwxyz])
and
[ABCDEFGHIJKLMNOPQRSTUVWXYZ])
From a cygwin session:-
AMIGA:~> while true; do read var; case "$var" in [abcdefghijklmnopqrstuvwxyz]) echo "Lower case alphabet $var" ;; [ABCDEFGHIJKLMNOPQRSTUVWXYZ]) echo "Upper case alphabet $var" ;; esac; done
a
Lower case alphabet a
A
Upper case alphabet A
f
Lower case alphabet f
F
Upper case alphabet F
K
Upper case alphabet K
k
Lower case alphabet k
Z
Upper case alphabet Z
z
Lower case alphabet z
AMIGA:~> _