I wrote script on Fedora (bash shell) to check whether a tring enter from user console is start with a uppercase/lowercase letter or a digit. But with this script i have some problem when I enter from character from 'b' to 'z' --> result is uppercase. This code look like ok but i don't know why it work not correctly. Please give me some propose.
#zdanie 3
echo "Enter a string:"
read var
echo ${var:0:1}
case ${var:0:1} in
[0-9])
echo "$var begin with a digit."
;;
[A-Z])
echo "$var begin with a uppercase letter."
;;
[a-z])
echo "$var begin with a lowercase letter."
;;
*)
echo "$var begin with another symbol."
;;
esac
echo "Enter a string:"
read var
echo ${var:0:1}
case ${var:0:1} in
[0-9]*)
echo "$var begin with a digit."
;;
[A-Z]*)
echo "$var begin with a uppercase letter."
;;
[a-z]*)
echo "$var begin with a lowercase letter."
;;
*)
echo "$var begin with another symbol."
;;
esac
Not sure why it didn't work with that syntax - the following should work. I put it into a while loop so you don't have to execute it after each input:
echo "Enter a string:"
while read var; do
echo ${var:0:1}
case ${var:0:1} in
[0-9]*)
echo "$var begin with a digit."
;;
[[:upper:]])
echo "$var begin with a uppercase letter."
;;
[[:lower:]])
echo "$var begin with a lowercase letter."
;;
*)
echo "$var begin with another symbol."
;;
esac
done
exit 0
The script seems to run in ksh shell. I was trying in my Linux machine running under shebang #!/bin/ksh where the script was able to recognize correctly the first character. However when tried to run under #!/bin/sh it gives the same result as the OP. If you have ksh, then could try that.
Note: ksh does not support string chopping (as in post# 1). So use cut or some other command to get the first letter