Updated April 10, 2023
Introduction to if condition shell script
Assume that in the workplace, you are responsible for the decision making of any process that is getting implemented at a production level. There would be some level of checks you would be taking to branch out the next steps of execution. Similarly, if condition also allows deciding to decide what is the next set of commands that needs to be executed if the condition is met. These conditions can themselves be part of a complex script or maybe resonated with loops to solve next level complex problems. The if condition imposes a lot of built-in checks and comparison tools which makes the condition building even easier. In these topics, we are going to learn about if condition in shell script.
Types of if condition in shell script
Now its time for understanding the types of if conditional statements.
1. Simple if statement
In this type of statement, only the if condition is used, which essentially means that the conditions in if conditions will be tested all along even if one of the conditions is satisfied. This type of condition is eventually used when the user wants to go through all the possible checks and not end the checks when one of the conditions is true.
Syntax:
if <expression for checking>;
then
<set of commands to be executed>
fi
2. If-Else condition
This type of statement is used when the program needs to check one condition and perform a task if the condition is satisfied or perform the other set of tasks if the condition is not.
Syntax:
if <expression for checking>;
then
<set of commands to be executed>
else
<set of other commands to be executed>
fi
3. If elif else fi statement
Starting from here, and the next type of if condition statement, the condition starts becoming branched. This type of statement is more like a ladder. If the condition is not satisfied one would feel like start climbing down the ladder and only stops if one condition is satisfied.
Syntax:
if <expression 1 for checking>;
then
<set of commands to be executed>
.
.
elif <expression 2 for checking>
then
<set of other commands to be executed>
.
.
else
<set of else set of commands to be executed>
fi
4. If then else … if then … fi
This is a nested else if statement and acts more like the if else statement. This type of statement is for adding another if condition within the else block.
Syntax:
if <expression 1 for checking>;
then
<set of commands to be executed>
.
.
else
if <expression 1 for checking>;
then
<set of commands to be executed>
.
.
fi
fi
Explanation
One thing we must be wary about is the indentation. Indentation is not essential for shell script but when it comes to best coding practice, it is always suggested to follow proper indentation. This is to make sure that another person can easily understand the code and in cases of maintenance of the code, it is super easy when indented. Now we are in a position to understand different operators supported by if conditions. Below listed are a few common operations which are widely used and hence become imperative to understand them as they constitute an important part.
Operation | What does it mean? |
! <Expression> | Expression is false |
-n <String 1> | The string length should be greater than 0 |
-z <String 1> | The string length is 0; or in other words, it is an empty string |
String A = String B | String A and String B are equal |
String A != String B | String A and B are not equal |
Integer 1-eq Integer 2 | Integer 1 is equal to Integer 2; Numerically |
Integer 1-gt Integer 2 | Integer 1 is less than Integer 2; Numerically |
Integer 1-lt Integer 2 | Integer 1 is less than Integer 2; Numerically |
-d <File> | To check if the <File> mentioned is present and if it is a directory |
-d <File> | To check if the <File> exists |
-d <File> | To check if <File> exists and has the permission granted for reading |
-d <File> | If the <File> exists and has a file size greater than 0 |
-d <File> | Checks if the <File> exist and has permission granted for write |
-d <File> | Checks if the <File> exist and has permission granted for execute |
Along with the above operation, if condition also supports usage of AND (&&) and OR (||) operators as well. Now using the above technical knowledge let us look at some examples in the next section.
Examples of if condition in shell script
In this section, we have sure to include examples which resemble real-time situation and also at the same time keep things simple to understand. Here, we would first look at the code understand that, and then look at the output of the code.
Example #1
Code:
echo "************************************************"
echo "Hello Fellas!"
echo "Demonstrating simple if condition"
echo "************************************************"
echo "Powered by EduCBA"
echo "################################################"
echo "Number entered is: $1"
rem=$(( $1 % 2 ))
if [ $rem -eq 0 ]
then
echo "Even Number"
else
echo "Odd Number"
fi
In the above code, the number is entered through the command line, and then the remainder is checked using a modulo operator and then equated with 0 to see if the remainder is 0 or not. In case of the remainder being 0, post-operation of modulo operator, the output is printed that number is even, else in other cases it is printed that number is odd.
Output:
Example #2
Code:
echo "************************************************"
echo "Hello Fellas!"
echo "Learn If condition with multiple branching!"
echo "************************************************"
echo "Powered by EduCBA"
echo "################################################"
for file in *;
do
if [ -f $f ]
then
echo "file : $file"
else
echo "dir : $file"
fi
done
echo
echo 'find learnIf.sh and Directory LearnIFDir'
for each_file in *;
do
if [ -f $each_file -o -d $each_file ]
then
if [ $each_file = 'learnIf.sh' ]
then
echo "File found: $file"
elif [ $each_file = 'LearnIFDir' ]
then
echo "Directory Found: $file"
else
echo " $each_file is neither a learIf.sh nor LearnIFDir”
fi
fi
done
In the above code, we look for all possible files and print their names and then in the next step we look some a specific file and a directory in the path location and print out if any of the file matches the ones we are looking for.
Output:
Conclusion
In this article, we have been able to give a complete vision of how if statements work and taking this as a baseline you are encouraged to build more of such script to test the understanding you have achieved from here. Even in your day to day professional work, you would be crossing similar kinds of tasks and these insights on how we write a script to achieve a result will go a long way in architecting the solution for the problem you are trying to solve!
Recommended Articles
This is a guide to if condition in shell script. Here we discuss the Types of if condition in shell script along with examples for better understanding. You may also look at the following articles to learn more –