This is a multipart blog article series where I am going to explain the concepts of shell scripting and how to write a shell script in Linux, UNIX or Mac based systems. You can also follow this tutorial blog using windows but for that you have to install a bash from.
In this article we will see an example to get a better understanding on the case statement concepts in shell scripting.
We are going to find that entered character is capital or small
- First we will ask for the input from the user, with the command
echo โe โEnter any character: \cโ
- Then we will store that value in a variable, named as value
- Now we will apply case statement, with different cases.
-
[a-z]: This means that we are expecting a small alphabet between
a-z
. -
[A-Z]: This means that we are expecting a capital alphabet between
A-Z
. -
[0-9]: This means that we are expecting an integer between
0-9
. - ? : This means that we are expecting any special character. (Only one special character)
-
[a-z]: This means that we are expecting a small alphabet between
- If your program doesnโt take or recognize the capital alphabets, then just type this command in terminal
LANG=C
Final code
echo -e "Please enter any character : \c"
read value
case $value in
[a-z] )
echo "User enter $value is in between a to z" ;;
[A-z] )
echo "User enter $value is in between A to Z" ;;
[0-9] )
echo "User enter $value is in between 0 to 9" ;;
? )
echo "User enter $value is a special character" ;;
* )
echo "Invalid input, this is maybe because you have entered more than one value." ;;
Reference code file for this article
So this was an example in which we had solved a problem using case statement in shell scripting. Hope you liked it and learned something new from it.
If you have any doubt, question, quires related to this topic or just want to share something with me, than please feel free to contact me.
๐ฑ Contact Me
Twitter
LinkedIn
Telegram
Instagram
Top comments (0)