Shell Script - Check whether a single character input is uppercase or lowercase or special character
Solution 1
Try using regular expression tests:
read -p "Type a character" c
if [[ "$c" =~ [a-z] ]]; then
echo "lowercase"
elif [[ "$c" =~ [A-Z] ]]; then
echo "uppercase"
else
echo "Non-alphabetic"
fi
Solution 2
Unless you empty $IFS
and add the -r
option, read
reads a line of input in a very special way.
For instance, if the user enters " \x "
, with the default value of $IFS
, $c
will contain x
, not what the user entered.
Also [a-z]
doesn't match lower case letters, it matches whatever sorts between a
and z
in the locale (with some variation in behaviour between shell. For instance, with bash
, in many locales, that includes the English letters between A and Y). It could even match sequences of characters in some locales and some tools.
Here, you'd probably want:
printf 'Please enter a character: '
IFS= read -r c
case $c in
([[:lower:]]) echo lowercase letter;;
([[:upper:]]) echo uppercase letter;;
([[:alpha:]]) echo neither lower nor uppercase letter;;
([[:digit:]]) echo decimal digit;;
(?) echo any other single character;;
("") echo nothing;;
(*) echo anything else;;
esac
(that syntax being POSIX sh
syntax, you don't even need to install bash
).
If you wanted to limit it to the English letters (letters from the latin script without diacritics), you'd need to either name them individually:
([abcdefghijklmnopqrstuvwxyz]) echo English lowercase letter;;
Or fix the locale to C with export LC_ALL=C
after the read
and before the case
statement, but then the (?)
test would be invalid as it could incorrectly interpret some character as sequence of characters. For instance a UTF-8 é
would be seen as two characters in the C locale.
Related videos on Youtube
FortuneCookie
Follow me at www.aravindmnair.blog Loves Solving Problems and engaging in Tech and Gadget related stuffs.
Updated on September 18, 2022Comments
-
FortuneCookie over 1 year
This is my code i have written. I need a simple code using if elif to check whether the character read is an uppercase,lowercase or a special symbol.
echo "enter a char" read c if [[ $c == [A-Z] ]]; then echo "upper" elif [[ $c == [a-z] ]]; then echo "lower" else echo "Digit or special symbols!" fi
The following is the output I received after inputting characters
enter a char A upper enter a char a Digit or special symbols! aravind@bionic-beaver:~/Desktop$ ./1.sh enter a char 1 Digit or special symbols!
-
Andy Dalton over 6 yearsYour script works for me as written (i.e., it correctly identifies
A
as uppercase,a
as lowercase, and1
as a digit or special symbol). -
FortuneCookie over 6 yearsWow..But how?? Are you sure you copied the same lines of code without any modification?
-
Andy Dalton over 6 yearsI copy/pasted your code without modification into
/tmp/ex
, then ranbash /tmp/ex
-
ilkkachu over 6 yearsRegardless of the fact that the locale setting will change the behaviour here, I do wonder how it's possible that
a
wouldn't be matched by[a-z]
, since it's listed as one of the endpoints. Unless you have extra spaces or other characters there, of course.
-
-
FortuneCookie over 6 yearsAnyway to implement the same using checking the ascii ranges like 65-90 for upper case and 97-122 for lowercase? or can you help with the same code itself as it doesnt validate the lowercase characters.
-
FortuneCookie over 6 yearscan you help me with the same code itself as it doesnt validate only the lowercase characters.? (without using regular expression)
-
ilkkachu over 6 yearsWell, there's also
shopt -s globasciiranges
in Bash, but of course it still doesn't make[a-z]
match anä
.