Shell Script - Check whether a single character input is uppercase or lowercase or special character

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












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!






share|improve this question


















  • 1




    Your script works for me as written (i.e., it correctly identifies A as uppercase, a as lowercase, and 1 as a digit or special symbol).
    – Andy Dalton
    Nov 27 '17 at 19:12










  • Wow..But how?? Are you sure you copied the same lines of code without any modification?
    – FortuneCookie
    Nov 27 '17 at 19:16










  • I copy/pasted your code without modification into /tmp/ex, then ran bash /tmp/ex
    – Andy Dalton
    Nov 27 '17 at 19:22










  • Regardless 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.
    – ilkkachu
    Nov 27 '17 at 20:25














up vote
1
down vote

favorite












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!






share|improve this question


















  • 1




    Your script works for me as written (i.e., it correctly identifies A as uppercase, a as lowercase, and 1 as a digit or special symbol).
    – Andy Dalton
    Nov 27 '17 at 19:12










  • Wow..But how?? Are you sure you copied the same lines of code without any modification?
    – FortuneCookie
    Nov 27 '17 at 19:16










  • I copy/pasted your code without modification into /tmp/ex, then ran bash /tmp/ex
    – Andy Dalton
    Nov 27 '17 at 19:22










  • Regardless 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.
    – ilkkachu
    Nov 27 '17 at 20:25












up vote
1
down vote

favorite









up vote
1
down vote

favorite











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!






share|improve this question














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!








share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '17 at 19:10









Andy Dalton

4,7941520




4,7941520










asked Nov 27 '17 at 19:04









FortuneCookie

255




255







  • 1




    Your script works for me as written (i.e., it correctly identifies A as uppercase, a as lowercase, and 1 as a digit or special symbol).
    – Andy Dalton
    Nov 27 '17 at 19:12










  • Wow..But how?? Are you sure you copied the same lines of code without any modification?
    – FortuneCookie
    Nov 27 '17 at 19:16










  • I copy/pasted your code without modification into /tmp/ex, then ran bash /tmp/ex
    – Andy Dalton
    Nov 27 '17 at 19:22










  • Regardless 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.
    – ilkkachu
    Nov 27 '17 at 20:25












  • 1




    Your script works for me as written (i.e., it correctly identifies A as uppercase, a as lowercase, and 1 as a digit or special symbol).
    – Andy Dalton
    Nov 27 '17 at 19:12










  • Wow..But how?? Are you sure you copied the same lines of code without any modification?
    – FortuneCookie
    Nov 27 '17 at 19:16










  • I copy/pasted your code without modification into /tmp/ex, then ran bash /tmp/ex
    – Andy Dalton
    Nov 27 '17 at 19:22










  • Regardless 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.
    – ilkkachu
    Nov 27 '17 at 20:25







1




1




Your script works for me as written (i.e., it correctly identifies A as uppercase, a as lowercase, and 1 as a digit or special symbol).
– Andy Dalton
Nov 27 '17 at 19:12




Your script works for me as written (i.e., it correctly identifies A as uppercase, a as lowercase, and 1 as a digit or special symbol).
– Andy Dalton
Nov 27 '17 at 19:12












Wow..But how?? Are you sure you copied the same lines of code without any modification?
– FortuneCookie
Nov 27 '17 at 19:16




Wow..But how?? Are you sure you copied the same lines of code without any modification?
– FortuneCookie
Nov 27 '17 at 19:16












I copy/pasted your code without modification into /tmp/ex, then ran bash /tmp/ex
– Andy Dalton
Nov 27 '17 at 19:22




I copy/pasted your code without modification into /tmp/ex, then ran bash /tmp/ex
– Andy Dalton
Nov 27 '17 at 19:22












Regardless 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.
– ilkkachu
Nov 27 '17 at 20:25




Regardless 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.
– ilkkachu
Nov 27 '17 at 20:25










2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










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





share|improve this answer




















  • Anyway 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
    Nov 27 '17 at 19:13











  • Not with regular expressions, but you could be able to convert the character to an ASCII code and do a numerical comparison if you really want to. That's beyond the scope of the original question however.
    – DopeGhoti
    Nov 27 '17 at 19:19










  • can you help me with the same code itself as it doesnt validate only the lowercase characters.? (without using regular expression)
    – FortuneCookie
    Nov 27 '17 at 19:22

















up vote
4
down vote













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.






share|improve this answer






















  • Well, there's also shopt -s globasciiranges in Bash, but of course it still doesn't make [a-z] match an ä.
    – ilkkachu
    Nov 27 '17 at 20:21










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f407356%2fshell-script-check-whether-a-single-character-input-is-uppercase-or-lowercase%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










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





share|improve this answer




















  • Anyway 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
    Nov 27 '17 at 19:13











  • Not with regular expressions, but you could be able to convert the character to an ASCII code and do a numerical comparison if you really want to. That's beyond the scope of the original question however.
    – DopeGhoti
    Nov 27 '17 at 19:19










  • can you help me with the same code itself as it doesnt validate only the lowercase characters.? (without using regular expression)
    – FortuneCookie
    Nov 27 '17 at 19:22














up vote
0
down vote



accepted










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





share|improve this answer




















  • Anyway 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
    Nov 27 '17 at 19:13











  • Not with regular expressions, but you could be able to convert the character to an ASCII code and do a numerical comparison if you really want to. That's beyond the scope of the original question however.
    – DopeGhoti
    Nov 27 '17 at 19:19










  • can you help me with the same code itself as it doesnt validate only the lowercase characters.? (without using regular expression)
    – FortuneCookie
    Nov 27 '17 at 19:22












up vote
0
down vote



accepted







up vote
0
down vote



accepted






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





share|improve this answer












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






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 27 '17 at 19:12









DopeGhoti

40.6k54979




40.6k54979











  • Anyway 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
    Nov 27 '17 at 19:13











  • Not with regular expressions, but you could be able to convert the character to an ASCII code and do a numerical comparison if you really want to. That's beyond the scope of the original question however.
    – DopeGhoti
    Nov 27 '17 at 19:19










  • can you help me with the same code itself as it doesnt validate only the lowercase characters.? (without using regular expression)
    – FortuneCookie
    Nov 27 '17 at 19:22
















  • Anyway 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
    Nov 27 '17 at 19:13











  • Not with regular expressions, but you could be able to convert the character to an ASCII code and do a numerical comparison if you really want to. That's beyond the scope of the original question however.
    – DopeGhoti
    Nov 27 '17 at 19:19










  • can you help me with the same code itself as it doesnt validate only the lowercase characters.? (without using regular expression)
    – FortuneCookie
    Nov 27 '17 at 19:22















Anyway 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
Nov 27 '17 at 19:13





Anyway 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
Nov 27 '17 at 19:13













Not with regular expressions, but you could be able to convert the character to an ASCII code and do a numerical comparison if you really want to. That's beyond the scope of the original question however.
– DopeGhoti
Nov 27 '17 at 19:19




Not with regular expressions, but you could be able to convert the character to an ASCII code and do a numerical comparison if you really want to. That's beyond the scope of the original question however.
– DopeGhoti
Nov 27 '17 at 19:19












can you help me with the same code itself as it doesnt validate only the lowercase characters.? (without using regular expression)
– FortuneCookie
Nov 27 '17 at 19:22




can you help me with the same code itself as it doesnt validate only the lowercase characters.? (without using regular expression)
– FortuneCookie
Nov 27 '17 at 19:22












up vote
4
down vote













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.






share|improve this answer






















  • Well, there's also shopt -s globasciiranges in Bash, but of course it still doesn't make [a-z] match an ä.
    – ilkkachu
    Nov 27 '17 at 20:21














up vote
4
down vote













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.






share|improve this answer






















  • Well, there's also shopt -s globasciiranges in Bash, but of course it still doesn't make [a-z] match an ä.
    – ilkkachu
    Nov 27 '17 at 20:21












up vote
4
down vote










up vote
4
down vote









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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 27 '17 at 20:17









Jeff Schaller

32.1k849109




32.1k849109










answered Nov 27 '17 at 19:30









Stéphane Chazelas

282k53521854




282k53521854











  • Well, there's also shopt -s globasciiranges in Bash, but of course it still doesn't make [a-z] match an ä.
    – ilkkachu
    Nov 27 '17 at 20:21
















  • Well, there's also shopt -s globasciiranges in Bash, but of course it still doesn't make [a-z] match an ä.
    – ilkkachu
    Nov 27 '17 at 20:21















Well, there's also shopt -s globasciiranges in Bash, but of course it still doesn't make [a-z] match an ä.
– ilkkachu
Nov 27 '17 at 20:21




Well, there's also shopt -s globasciiranges in Bash, but of course it still doesn't make [a-z] match an ä.
– ilkkachu
Nov 27 '17 at 20:21

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f407356%2fshell-script-check-whether-a-single-character-input-is-uppercase-or-lowercase%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?