Shell Script - Check whether a single character input is uppercase or lowercase or special character
Clash 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!
bash shell-script shell scripting
add a comment |Â
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!
bash shell-script shell scripting
1
Your script works for me as written (i.e., it correctly identifiesA
as uppercase,a
as lowercase, and1
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 ranbash /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 thata
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
add a comment |Â
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!
bash shell-script shell scripting
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!
bash shell-script shell scripting
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 identifiesA
as uppercase,a
as lowercase, and1
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 ranbash /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 thata
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
add a comment |Â
1
Your script works for me as written (i.e., it correctly identifiesA
as uppercase,a
as lowercase, and1
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 ranbash /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 thata
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
add a comment |Â
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
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
add a comment |Â
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.
Well, there's alsoshopt -s globasciiranges
in Bash, but of course it still doesn't make[a-z]
match anä
.
â ilkkachu
Nov 27 '17 at 20:21
add a comment |Â
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
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
add a comment |Â
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
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
add a comment |Â
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
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
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
add a comment |Â
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
add a comment |Â
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.
Well, there's alsoshopt -s globasciiranges
in Bash, but of course it still doesn't make[a-z]
match anä
.
â ilkkachu
Nov 27 '17 at 20:21
add a comment |Â
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.
Well, there's alsoshopt -s globasciiranges
in Bash, but of course it still doesn't make[a-z]
match anä
.
â ilkkachu
Nov 27 '17 at 20:21
add a comment |Â
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.
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.
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 alsoshopt -s globasciiranges
in Bash, but of course it still doesn't make[a-z]
match anä
.
â ilkkachu
Nov 27 '17 at 20:21
add a comment |Â
Well, there's alsoshopt -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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Your script works for me as written (i.e., it correctly identifies
A
as uppercase,a
as lowercase, and1
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 ranbash /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