If else or decision statements or conditional statements are one of the important parts of the Shell Scripting. As in any programming language, they help in deciding the flow of execution of the shell script. It gives you a way to analyze the results, analyze the exit codes, or analyze the intermediary state of a variable and take a decision in a script. In our last post Command Line Arguments and Exit Status in Shell Scripting we gave a little introduction about conditional statements uses. Go through it if you haven’t and get a taste of this little knowledge byte.
If else statement is the basic conditional statement which checks if the condition given is met it will execute the commands following if statement or else execute the commands following else statement.
if condition then condition is true, execute commands between then and else, we will call it as part 1 else condition is false, execute commands between else and fi, we will call it as part 2 fi
We just saw an if else statement’s syntax above. Till now you might have guessed the syntax but if not then we will explain it here:
if – starting of if else syntax.
condition – the condition which needs to be checked to know which part of code has to be executed.
then – starting of part 1 of code which executes when condition results true.
else – end of part 1 of code and starting of part 2 of code which executes when condition results false.
fi – end of part 2 of code.
So we got to know that only one of the two parts can get executed, depending on the condition statement’s result. But what if we have only one part to execute. In that case we have If condition only without else.
if condition then condition is true, execute commands between then and fi fi
That was quite simple. You would have noticed that, we are just testing that condition is true or not. If true then execute the commands between then and fi. No else statement exists, hence no other part for false condition.
Let’s take it to advance level. We will now talk about nested if else. There might be some condition which you want to test if some other condition has became true or may be false. That’s where nesting if else with some other if else will help.
if condition1 then if condition2 then condition1 and condition2 is true, execute commands between then and else, we will call it as part1 else condition1 is true and condition2 is false, execute commands between else and fi, we will call it as part2 fi else condition is true, execute commands between then and fi, we will call it as part3 fi
And yes you can have only if conditional statement inside if or else of outer if else conditional statement. Here in above pseudo code, we are checking if condition 1 is true then check for condition 2 if it is true execute statements in part1, if condition true is wrong execute statements in part2. Let’s say the condition1 was false, part3 would have been executed. Nested statements can be considered as one unit of code inside other unit, just be careful, too much of nesting is not wise thing to do, as it will effect the program’s execution speed.
Now comes the Multilevel if else, or if else if ladder. This gives us a way to have more then one condition check in a decision statement. Helpful in many cases where state of variable can be more than two.
if condition1 then condition1 is true, execute commands between then and elif, we will call it as part1 elif condition2 then condition2 is true, execute commands between then and elif, we will call it as part2 elif condition3 then condition3 is true, execute commands between then and elif, we will call it as part3 else None of the condition tested is true, execute commands between else and if, we will call it as part4 fi
elfi – end of part1 code, and start of another else if statement. Its the boundary where else if meets and if first condition is false, condition following elfi is checked.
The ladder keeps on checking for the favorable condition until favorable condition is met, or last part is executed. This helps us to access inputs or outputs of some other functions which can have multiple states. For example, if exit code of another programs could be anything between 0-5, we can check it using if else ladder.
That’s all for if else. Try it at home 😉 and let us know if you need any help. Please comment below for same, or connect with us on Facebook.