Here i will be writing a program using IF-ELSE condition and making a simple calculator.
We use if-else in shell scripts when we wish to evaluate a condition, then decide to execute one set between two or more sets of statements using the result.
#!/bin/bash
#-----------------------------------------
#Created by: Ajay
#Purpose: simple calculator
#-----------------------------------------
#colors
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
nc='\033[0m'
#defining the 2 terms
read -p $'\e[33mEnter number 1\e[0m: ' num1
read -p $'\e[33mEnter number 2\e[0m: ' num2
#to find which function to perform
echo ""
echo " Option for operation choices "
echo " 1.addition "
echo " 2.subtraction "
echo " 3.multiplication "
echo " 4.division "
echo ""
#read the selected option
read -p " Enter the choice of operation: " S1
echo ""
#if condition for selected options
if [ $S1 -eq 1 ]
then
echo -e "${green}The result will be:${nc} " $(( $num1 + $num2 ))
elif [ $S1 -eq 2 ]
then
echo -e "${green}The result will be:${nc} " $(( $num1 - $num2 ))
elif [ $S1 -eq 3 ]
then
echo -e "${green}The result will be:${nc} " $(( $num1 * $num2 ))
elif [ $S1 -eq 4 ]
then
echo -e "${green}The result will be:${nc} " $(( num1 / num2 ))
else
echo -e "${red}wrong operation ${nc}"
fi

