🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Shell Scripting Tutorials: Conditional statatement using if


Conditional Statements: There are total 5 conditional statements which can be used in bash programming

if statement
if-else statement
if..elif..else..fi statement (Else If ladder)
if..then..else..if..then..fi..fi..(Nested if)
switch statement






Here are some useful examples of if-else in shell scripts to give you a better idea of how to use this tool.

CommandDescription
&&Logical AND
$0Argument 0 i.e. the command that’s used to run the script
$1First argument (change number to access further arguments)
-eqEquality check
-neInequality check
-ltLess Than
-leLess Than or Equal
-gtGreater Than
-geGreater Than or Equal

# ############################################
# Program - 1 - Format
# ############################################
if [condition]
then
statement1
else
statement2
fi
# ############################################
# Program - 2 - Using if-else to check whether two numbers are equal
# ############################################
#!/bin/bash
m=1
n=2
if [ $n -eq $m ]
then
echo "Both variables are the same"
else
echo "Both variables are different"
fi
# ############################################
# Program - 3 - Using if-else to compare two values
# ############################################
#!/bin/bash
a=2
b=7
if [ $a -ge $b ]
then
echo "The variable 'a' is greater than the variable 'b'."
else
echo "The variable 'b' is greater than the variable 'a'."
fi
# ############################################
# Program - 4 - Using if-else to check whether a number is even
# ############################################
#!/bin/bash
n=10
if [ $((n%2))==0 ]
then
echo "The number is even."
else
echo "The number is odd."
fi
# ############################################
# Program - 5 - Using if-else as a simple password prompt
# ############################################
#!/bin/bash
echo "Enter password"
read pass
if [ $pass="password" ]
then
echo "The password is correct."
else
echo "The password is incorrect, try again."
fi
# ############################################
# Program - 6 - if-elif-if.sh
# ############################################
#!/bin/bash
#Purpose: Find biggest Number among 4 digits
# START #
echo -e "Please Enter a Value: \c"
read -r a
echo -e "Please Enter b Value: \c"
read -r b
echo -e "Please Enter c Value: \c"
read -r c
echo -e "Please Enter d Value: \c"
read -r d
if [ $a -gt $b -a $a -gt $c -a $a -gt $d ]; then
echo "$a a is big"
elif [ $b -gt $c -a $b -gt $d ]; then
echo "$b b is big"
elif [ $c -gt $d ]; then
echo "$c c is big"
else
echo "$d d is big"
fi
# END #
# ############################################
# Program - 7 - if-elif-if.sh
# ############################################
#!/bin/bash
#Purpose: If else statement example
# START #
echo -e "Enter any value: \c"
read -r a
echo -e "Enter any value: \c"
read -r b
if [ $a -gt $b ]; then
echo "$a is greater than $b"
else
echo "$b is greater than $a"
fi
# END #
# ############################################
# Program - 8 - if-elif-if.sh
# ############################################
#!/bin/bash
#Purpose: If statement example
# START #
echo -e "Please provide Value below ten: \c"
read -r value
if [ $value -le 10 ]
then
echo "You provided value is $value"
touch /tmp/test{1..100}.txt
echo "Script completed successfully"
fi
# ############################################
# Program - 9 - Nested If
# ############################################
#!/bin/bash
#Purpose: Validate and report Student subject marks
echo -e "Please Enter Maths Marks: \c"
read -r m
echo -e "Please Enter Physics Marks: \c"
read -r p
echo -e "Please Enter Chemistry Marks: \c"
read -r c
if [ $m -ge 35 -a $p -ge 35 -a $c -ge 35 ]; then
total=`expr $m + $p + $c`
avg=`expr $total / 3`
echo "Total Marks = $total"
echo "Average Marks = $avg"
if [ $avg -ge 75 ]; then
echo "Congrats you got Distinction"
elif [ $avg -ge 60 -a $avg -lt 75 ]; then
echo "Congrats you got First Class"
elif [ $avg -ge 50 -a $avg -lt 60 ]; then
echo "You got second class"
elif [ $avg -ge 35 -a $avg -lt 50 ]; then
echo "You Got Third Class"
fi
else
echo "Sorry You Failed"
fi
# END #
Subscribe
Notify of
guest


0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x