Skip to main content

Command Palette

Search for a command to run...

Case statements used in Shell Script

Published
3 min read
Case statements used in Shell Script
M

Hi, How are you !! Hope you doing good....

I got introduced to Cloud initially. As I went ahead learning what is cloud and how it works, then got to know a field which is DevOps that makes Cloud model more effective.

So, as I started working & got good experience on AWS. I have been learning the DevOps tool and technologies on how to use it with the Cloud, which will give me good understanding on how Cloud and DevOps go hand in hand to deploy my applications.

Last Blog Review

In the last blog we understood, why WHILE LOOP is used and it’s syntax along with some examples of using WHILE LOOP.

1. What are Case Statements →

The if condition block with a lot of if-else statement can be written in better way using "case statement".

2. Case statement syntax →

case <<value>> in
        1) .... ;;
        2) .... ;;
        *) .... ;;
esac

3. Shell Script example using case statements

a. Shell Script to print the month by giving number as inputs

i-002978a646c30dd8b (EC2)
-------------------------
-lp10:~$ vim print-month-name.sh 
-lp10:~$ cat print-month-name.sh    
month_number=$1

if [ -z $month_number ]
then
  echo "No month number given. Please enter a month number as a command line argument."
  echo "eg: ./print-month-number 5"
  exit
fi

if [[ $month_number -lt 1 && $month_number -gt 12 ]]
then
  echo "Invalid month number given. Please enter a valid number - 1 to 12."
  exit
fi

case $month_number in
  1) echo "January" ;;
  2) echo "February" ;;
  3) echo "March" ;;
  4) echo "April" ;;
  5) echo "May" ;;
  6) echo "June" ;;
  7) echo "July" ;;
  8) echo "August" ;;
  9) echo "September" ;;
  10) echo "October" ;;
  11) echo "November" ;;
  12) echo "December" ;;
esac
-lp10:~$ chmod 700 print-month-name.sh 
-lp10:~$ ./print-month-name.sh       
No month number given. Please enter a month number as a command line argument.
eg: ./print-month-number 5
-lp10:~$ ./print-month-name.sh 7
July
-lp10:~$

b. Shell script for implementing calculator using case statements

i-002978a646c30dd8b (EC2)
-------------------------
-lp10:~$ vim calculator.sh 
-lp10:~$ cat calculator.sh 
#!/bin/bash
while true
do
  echo "1. Average"
  echo "2. Subsctract"
  echo "3. Multiply"
  echo "4. Divide"
  echo "5. Quit"

  read -p "Enter your choice: " choice

case $choice in
   1)
      read -p "Enter Number1: " number1
      read -p "Enter Number2: " number2
      sum=$(( number1 + number2 ))
      echo Answer=$(echo "$sum / 2" | bc -l)
      ;;
   2)
      read -p "Enter Number1: " number1
      read -p "Enter Number2: " number2
      echo Answer=$(( $number1 - $number2 ))
      ;;

    3)
      read -p "Enter Number1: " number1
      read -p "Enter Number2: " number2
      echo Answer=$(( $number1 * $number2 ))
      ;;
    4)
      read -p "Enter Number1: " number1
      read -p "Enter Number2: " number2
      echo Answer=$(( $number1 / $number2 ))
      ;;
    5)
      break
      ;;
  esac
done
-lp10:~$ chmod 700 calculator.sh 
-lp10:~$ ./calculator.sh 
1. Average
2. Subsctract
3. Multiply
4. Divide
5. Quit
Enter your choice: 2
Enter Number1: 5
Enter Number2: 1
Answer=4

Conclusion

In the this blog we understood, the use of case statements to use instead of using multiple if else statements along with its syntax and shell scripts.

💡
That’s a wrap for today’s post! I hope this has given you some valuable insights. Be sure to explore more articles on our blog for further tips and advice. See you in the next post!

More from this blog

Mihir's Tech blog

69 posts