Recent Posts

ads

Bash script -5 If-Them Statement








If Statement
A basic if statement effectively says, if a particular test is true, then perform a given set of actions. If it is not true then don't perform those actions




if [ <regular test> ]
then
<commands>
fi





Understanding conditional statement


if
Perform a set of commands if a test is true.
else
If the test is not true then perform a different set of commands.
elif
If the previous test returned false then try this one.
&&
Perform the and operation.
||
Perform the or operation.
case
Choose a set of commands to execute depending on a string matching a particular pattern.


Example 1
This is what we have in our terminal. We created a bash file called checking_age








To explain this we will need to explain bash operators and our script in nano text editor:

Bash OperatorOperatorDescription
-eq==Equal
-ne!=Not equal
-gt>Greater than
-ge>=Greater than or equal
-lt<Less than
-le<=Less than or equal
-z== nullIs null



Nano text editor


Example #2

Let give you a better example with if statement

if [ <something happens> ]
then
<commands>
fi











If Else

Sometimes we want to perform a certain set of actions if a statement is true, and another set of actions if it is false. We can fix this with the else statement.

if [ <something happens> ]
then
<commands>
else
<other commands>
fi

 Elif Else

Sometimes we may have different  conditions that may lead to different paths.
if [ <something happens> ]
then
<commands>
elif [ <something else happens> ] 
then
<different commands>
else
<other commands>
fi

Example #3



  • = is  different to -eq. [ 001 = 1 ] will return false as = does a string comparison  whereas -eq does a numerical comparison meaning [ 001 -eq 1 ] will return true.


This is the results






But in case you want to add something different from a number (integer)



Example #4

Now we will see an example with if, elif and else





The result will be this




Boolean Operations

Sometimes we only want to do something if multiple conditions are met. Other times we would like to perform the action if one of several conditions is met. We can accommodate these with boolean operators.
  • and - &&
  • or - ||
&& is a way of expressing Logical AND, meaning the entire expression is True only if both sides of it are true. In logic, it is said the entire statement (P&Q) is only true when both P and Q are true.


Example #5

We will create a bash file called example













Case Statements


Sometimes we may wish to take different paths based upon a variable matching a series of patterns. We could use if and elif statements but that would get humongous eventually. Fortunately, there is a case statement which can make things nicer. 
We will provide examples

case <variable> in
<pattern 1>)
<commands>
;;
<pattern 2>)
<other commands>
;;
esac



We will create a bash script called example2





I hope this information was useful, and if it was and you like it, please click like!

Thanks for watching this
Bash script -5 If-Them Statement Bash script -5 If-Them Statement Reviewed by ohhhvictor on May 30, 2019 Rating: 5

No comments:

Facebook

ads
Powered by Blogger.