bash - How to check if the user has entered a single letter? -
i reading character , want check if single character , letter. code below:
#!/usr/bin/bash read -p "enter something: " char if [[ ${#char} != 1 && "$char" != *[a-z]* ]]; echo "not valid input" else echo "its valid input" fi
the o/p below:
[root@host-7 ~]# sh -x t + read -p 'enter something: ' char enter something: 1 + [[ 1 != 1 ]] + echo 'its valid input' valid input
while executing script first condition getting executed , not checking second condition.
it not evaluating 2nd condition because first condition failing you're entering 1 character in input , there &&
between 2 conditions.
if enter 2 character input ab
you'll see both conditions getting evaluated.
you can use -n1
restrict input 1 character this:
#!/usr/bin/bash read -n1 -p "enter something: " char echo if [[ "$char" != *[a-z]* ]];then echo "not valid input" else echo "its valid input" fi
and run as:
bash -x ./t
Comments
Post a Comment