Bash Shell Scripting:- Lesson 7,8 &9 for DevOps Engineers.
Dear Learners, In Today Topic, We will learn the Shell Scripting form Basics to Advanced for DevOps Engineers.
Course Content:
Lesson 7:- Bash Shell Scripting Nested IF Condition in Shell Script.
Lesson 8:- Bash Shell Scripting Using Logical and Operator in Shell Script.
Lesson 9:- Bash Shell Scripting Using Logical OR Operator in Shell Script.
IMP We are Dividing the Terminal in 2 Parts Right Side Script Execution and Left Side Write a bash file with Syntax.
Lesson 7:- Bash Shell Scripting Nested IF Condition in Shell Script.
Nested IF function means one if function inside the another allows you to multiple criteria and increase the Number of possible outcomes.
Let us discuss with the help of practical:-
To check the file is available or not:-
output:- does not exist
we are executing the script with read permissions, write permission.
#!/bin/bash
# Nested IF Condition in shell Scripts"
echo -e "Please Enter the name of file \c"
read filename
# to check the file is available or not
if [ -f $filename ]
then
if [ -w $filename ]
then
echo "please type your test here & press ctrl+d to exit"
cat >> $filename
else
echo "$filename does not have the write permission on it"
fi
else
echo "$filename does not exists"
fi
To check if file is available in system and Append the Text.
we are executing the script with read permissions, write permission.
#!/bin/bash
# Nested IF Condition in shell Scripts"
echo -e "Please Enter the name of file \c"
read filename
# to check the file is available or not
if [ -f $filename ]
then
if [ -w $filename ]
then
echo "please type your test here & press ctrl+d to exit"
cat >> $filename
else
echo "$filename does not have the write permission on it"
fi
else
echo "$filename does not exists"
fi
Lesson 8:- Bash Shell Scripting Using Logical AND Operator in Shell Script.
Logical AND Operator in shell operator:-
If the both conditions are high and the equal to each other we will get the output for ex:- 1 = 1
Logical AND Operators
Input output
X Y Z
0 0 0
0 1 0
1 0 0
1 1 1
if the age is valid
Let us discuss with the help of age example.
age will be greater than 18 or age will be less than 40
Synatx:-
#!/bin/bash
# Logical AND Operator &&in shell Scripts"
echo -e "Please Enter the age \c"
read age
if [ "$age" -gt 18 ] && [ "$age" -lt 40 ]
then
echo "age is valid"
else
echo "age is not valid"
fi
if the age is not valid
et us discuss with the help of age example.
age will be greater than 18 or age will be less than 40
Synatx:-
#!/bin/bash
# Logical AND Operator &&in shell Scripts"
echo -e "Please Enter the age \c"
read age
if [ "$age" -gt 18 ] && [ "$age" -lt 40 ]
then
echo "age is valid"
else
echo "age is not valid"
fi
Lesson 9:- Bash Shell Scripting Using Logical OR Operator in Shell Script.
In this lecture we will discuss the logical OR operator how to implement in shell scripting to define the different different conditions.
it is used to define the either the one condition is true system will not go to the second. if ones work it will execute if 1st wrong it will move to the second.
for example:- compare the the 2 commands:-
cal || zdate
zdate || cal
Let us discuss with the help of Script:-
Compare the Age in these Script.
if the age is less than 18 || greater than 40
#!/bin/bash
# Logical OR Operator &&in shell Scripts"
echo -e "please enter the age: /c"
read age
if [ "$age" -lt 18 ] || [ "$age" -gt 40 ]
then
echo "age is valid true"
else
echo "age is not valid"
fi
Another Method You are Execute the Script with the help -o with single Quotes.
#!/bin/bash
# Logical OR Operator &&in shell Scripts"
echo -e "please enter the age: /c"
read age
if [ "$age" -le 18 -o "$age" -ge 40 ]
then
echo "age is valid true"
else
echo "age is not valid"
fi
Hope I helped you in understanding the concept in a better way!!
Happy Learning !!
CKA certified | REDHAT OPENSTACK Certified |GCP-Associate cloud engineer certified| Docker | kubernetes | AzureDevOps | AWS | Linux | ansible | cicd | jenkins | terraform | Grafana | GIT |GITHUB
1yThanks for sharing Maninder Singh
AWS || Huawei || Cloud || Docker || Mentor & Tutor || System Engineer Antier Solution
1y++