If-else simple understanding in scripts

Mihir SuratwalaMihir Suratwala
3 min read

Last Blog Review

In the last blog we understood, what if conditional logic and how to write the syntax for if—else statement in shell script. Also we checked how the different operators for checking the conditions are used to get perform if-else.

If-Else shell script to understand how to use it.

Shell script 1.

The script should check the greater of the two command line arguments $1 and $2 and print which ever is greater

i-002978a646c30dd8b (EC2)
-------------------------
bob@caleston-lp10:~$ vim check_greater.sh
bob@caleston-lp10:~$ cat check_greater.sh 
#!/bin/bash

if [ $1 -gt $2 ]
then
        echo "$1"
else
        echo "$2"
fi
bob@caleston-lp10:~$ chmod 700 check_greater.sh
bob@caleston-lp10:~$ ./check_greater.sh 5 10
10
bob@caleston-lp10:~$ ./check_greater.sh 5 1 
5
bob@caleston-lp10:~$

Shell Script 2.

Develop a shell script that accepts the number of a month as input and prints the name of the respective month. eg ./print-month-name.sh 1 should print January and ./print-month-name.sh 5 should print May. Also keep these in mind.

1) The script must accept a month number as a command line argument. If a month number is not provided as command line argument, the script must exit with the message No month number given.

2) The script must not accept a value other than 1 to 12. If not the script must exit with the error Invalid month number given

Answer -

-z $1 is used to check for command line argument. The -z option is used to test whether the variable "string" is empty so if the variable is empty the script executes the code in "if" block and if it is not empty the code in "else" block is executed.

i-002978a646c30dd8b (EC2)
-------------------------
bob@caleston-lp10:~$ vim print-month-name.sh
bob@caleston-lp10:~$ cat print-month-name.sh 
#!/bin/bash

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

if [ $month_number -eq 1 ]
then
  echo "January"
elif [ $month_number -eq 2 ]
then
  echo "February"
elif [ $month_number -eq 3 ]
then
  echo "March"
elif [ $month_number -eq 4 ]
then
  echo "April"
elif [ $month_number -eq 5 ]
then
  echo "May"
elif [ $month_number -eq 6 ]
then
  echo "June"
elif [ $month_number -eq 7 ]
then
  echo "July"
elif [ $month_number -eq 8 ]
then
  echo "August"
elif [ $month_number -eq 9 ]
then
  echo "September"
elif [ $month_number -eq 10 ]
then
  echo "October"
elif [ $month_number -eq 11 ]
then
  echo "November"
elif [ $month_number -eq 12 ]
then
  echo December
fi
bob@caleston-lp10:~$ chmod 700 print-month-name.sh 
bob@caleston-lp10:~$ ./print-month-name.sh 
No month number given. Please enter a month number as a command line argument.
eg: ./print-month-number 5
bob@caleston-lp10:~$ ./print-month-name.sh 0
Invalid month number given. Please enter a valid number - 1 to 12.
bob@caleston-lp10:~$ ./print-month-name.sh 100
Invalid month number given. Please enter a valid number - 1 to 12.
bob@caleston-lp10:~$ ./print-month-name.sh 9
September

Conclusion →

In the this blog we understood, simple shell scripts to use if-else to get the work done.

💡
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!
0
Subscribe to my newsletter

Read articles from Mihir Suratwala directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mihir Suratwala
Mihir Suratwala

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.