Check whether a string begin with uppercase, lowercase or digit!

Hi every body!

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

There are some wildcards missing:

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

Tks for your reply, but the same problem when I enter character from 'b' -> 'z' the result is uppercase too.

Ah ok understood and recognized it now - will check for a solution.

Try this,

#!/bin/sh

echo -n "Enter a string:"
read var
first_char=${var:0:1}
len=$(expr length $(echo "$first_char" | tr -d "0-9") 2>/dev/null)
if [ $len > 0 ]
then
        len=$(expr length `echo $first_char | tr -d "a-z"` 2>/dev/null)
        if [ $len > 0 ]
        then
                len=$(expr length `echo $first_char | tr -d "A-Z"` 2>/dev/null)
                if [ $len > 0 ]
                then
                        echo "Something else"
                else
                        echo "Uppercase"
                fi
        else
                echo "lowercase"
        fi
elif [ $first_char -ge 0 2>/dev/null ]
then
echo "Number"
else
echo "Something else"
fi
1 Like

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
1 Like

Ok. now it worked correctly! Tks so much for your help! My friends.

--- deleted ---

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

1 Like
read var
t=${var#?}
case ${var%$t} in
...

---------- Post updated at 10:58 AM ---------- Previous update was at 10:56 AM ----------

# var=Tstdjslqggfdskgmds
# t=${var#?}
# echo ${var%$t}
T