Thursday, November 5, 2020

Shell Script - If statemenet

 If statements allow us to make decisions based on condition in our code.
It allow us whether to run particular statement if certain condition met.

We can combine if statement with loops to solve complex conditions.


<SYNTAX>
if [ <some condition> ]
then
  <Statement1>
fi

Here, if a condition is true, then perform statement1 or all the steps within statement1 that mean within then and fi.
If this statement is not true, skip statement1. Don't do anything within that block.
in detail, if the condition between square brakets [ .. ] is true, execute statement between then and fi.

Lets look at an example:


#!/bin/bash
# If else statement
#
if [ $1 -eq 5 ]
then
   echo "You won a lottery."
   echo "Life is beautiful !!!"
   echo "You are lucky person"
fi

echo "Testing if statement"

In this example,
- user enters a value as command line parameter and value is assigned to $1.
- $1 is compared to 5. if value of $1 is equal to 5 then
  the statement between then and fi will execute.
- and Also the echo statement after fi will also execute.
- If $1 is not equal to 5 or False, it will not the block between then and fi. it skips it.
- The control will print the echo statement after fi since it is out side of if statement.



if else
-------
When you have two option to choose with certain condition, you can use if else. If a statement is true, run the statement1, if it is falso, do not run that block, move it to else part.
<SYNTAX>

if [ < Some Condition> ]
then
   <Statement1>
else
   <statement2>
fi

in this example,
if the condition is true then it will run statement1. If it is not true or False, it will skip statement and goes to else part. And executes the statement2 block.

#!/bin/bash
# if else statement
#

if [ $i -eq 5 ]
then
  echo "Number you entered is 5"
else
  echo "Number you entered is not 5"
fi

Here, user enters 5 and if condition checks if the number is equal to 5.
if it is true, then it prints number you entered is 5.
if this is not true, that is False, it will not run this block
and control moves to else part.
and else part will be printed.

if elif else
-------------
Now, lets say you have multiple condition (option) to choose from, you will go with if elif else.
<syntax>
if [ < Some condition> ]
then
  echo "Statement1"
elif [ < Another condition> ]
then
  echo "Statement2"
else
  echo " Other statement"
fi

Here, if the condition is true, it will execute statement1
if the condition is not true that is false, then this block will not run, it skips.
control goes to elif section.
here, if this condition is true, runs the code (Statement2) within the block.
if condition is false, it will not run this block and moves cursor to next line.
which is else part.
Else part will be executed and prints the statement "Other statement".

Lets see an example.
#!/bin/bash
# if efif else statement
#

if [ $1 -eq 5 ]
then
  echo "Need special attention"
elif [ $1 -lt 18 ]
then
  echo "Be careful"
else
  echo "Its your world !!!"
fi

Here, if condition will check if the value user entered is 5 (True), then return
need special attention
if this condition is false, do not run this block. control moves to elif.
Here, if the user supplied value is less than 18, then it will print
be careful
if this condition is false, it will not run this part and cursor moves to else part.
and it prints
Its your world !!!


There are some operators that go along with if statements.
When you have multiple conditions, these operatorss are very userful.
-> and - &&  - Two ampersand    # both condition need to be TRUE
-> or  - ||  - two piped lines  # only condition need to be TRUE

These operators return True or False values. Thats why we call them boolean operators.

Lets say, you go to grocery store and need to buy bread. You have a condition,
buy bread, only if there is a Jam.
if Jam is not there, do not buy bread. In this condition you use and (&&)

lets see other condition,
You go to store and buy orange. if orange is not there buy apple.

So buy orange or apply -> in this case you don't have to buy both, buy only one.



No comments:

Post a Comment

Git branch show detached HEAD

  Git branch show detached HEAD 1. List your branch $ git branch * (HEAD detached at f219e03)   00 2. Run re-set hard $ git reset --hard 3. ...