Hello.... Friends today we are going to discus about the conditional statements in the shell scripting . like in other languages the Shell Programming too have conditionals statements . Conditional executions means executing code when certain conditions met and during conditional executions we just simply executes our multiple commands one after the other.
Conditional statements are basically
Conditional statements are basically
- if/then
- else
- elif
Let's understand who & where we can use them. these are pretty Simple to use. So Let's understand them one by one.
- if/then
if [ condition ] # when the condition is true then statement is executed or else it return.
then
statements
fi
Major things we have to remember the space after starting & before closing the Brackets. & closing of if statement with fi command.
- else
Syntax :
if [ condition ] # if the condition is true
then
true statements # statement is executed
else
false statements # statement is executed
fi
But if we want to make some more sense of the same script then we need to use else conditional statement too.
if [ condition ] # if the condition is true
then
true statements # statement is executed
else
false statements # statement is executed
fi
But if we want to make some more sense of the same script then we need to use else conditional statement too.
i e as we put false condition then it simply returns. but as we put else condition too then let's check what happened
- elif
Syntax :
if [ condition ] # if condition is true
then
true statement
elif [ either condition ] # either different but true conditions
true statement
else
false statement # if all the conditions are false
fi
i think you will understand by this syntax where the elif statement is used so elif statement is generally for multiple true statements but we don't know the write one so to test the condition that which condition is meeting to our condition then we can use this elif conditional statement. it will avoid the multiple if statement in our script.
let's clear it with our script
if you have different OS then try this script in your shell. I hope it will provide you the write result as per your OS.
thanks friends , Please leave your comments & like Tutorials it will help me in improving my quality.
No comments:
Post a Comment