You are reading the article If Condition In Shell Script updated in September 2023 on the website Saigonspaclinic.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 If Condition In Shell Script
Introduction to if condition shell scriptAssume 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.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Types of if condition in shell script 1. Simple if statementIn 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:
fi
2. If-Else conditionThis 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:
fi
3. If elif else fi statementStarting 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:
fi
4. If then else … if then … fiThis 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:
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 is false
The string length should be greater than 0
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
Examples of if condition in shell scriptIn 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 #1Code:
if [ $rem -eq 0 ] 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.
Example #2Code:
if [ -f $f ] if [ -f $each_file -o -d $each_file ] if [ $each_file = ‘learnIf.sh’ ] elif [ $each_file = ‘LearnIFDir’ ] 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:
ConclusionIn 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 ArticlesThis 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 –
You're reading If Condition In Shell Script
Update the detailed information about If Condition In Shell Script on the Saigonspaclinic.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!