DEV Community

Cover image for Case Statement Example | Shell Scripting
Rahul Mishra
Rahul Mishra

Posted on • Originally published at programmingport.hashnode.dev

Case Statement Example | Shell Scripting

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

  1. First we will ask for the input from the user, with the command echo –e “Enter any character: \c”
  2. Then we will store that value in a variable, named as value
  3. 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)
  4. 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." ;;
Enter fullscreen mode Exit fullscreen mode

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

📧 Write a mail

rahulmishra102000@gmail.com

🚀 Other links

GitHub
HackerRank

Top comments (0)