Hello friends.
Welcome again to the part two of Fundamentals of Linux Shell Scripting.
In this part we are going to talk about the control statements and functions in Linux shell scripting.
But before actually moving to this video, I want you to practice the exercises which are provided after
the first video.
Okay.
And if you have already practiced the exercises then let's move to this video.
Okay.
So first of all let's see what is the control statement.
Control statements in Linux shell scripting are essential for creating complex and dynamic scripts.
They allow you to control the flow of execution based on conditions, loops, and other criteria.
Okay, we can write conditions.
Okay?
We can perform repetition of script with the help of loops okay.
And by using conditions and loops we can control the flow of The execution of our scripts so we can
write different conditions by combining different operators like string operators, Boolean operators,
relational operators, arithmetic operators.
Okay.
And the last one is file test operators.
So the control statements can be categorized into different types.
So let's see the first type.
The first type is conditional statements.
In conditional statements we use the keyword if else if else if else to control the flow of execution
okay.
And we can write different conditions okay.
With the help of these keywords.
The if else and if else if else is called as the keywords.
Okay.
The meaning of these keywords is already known to our shell.
Okay.
So we can write different conditions by using these keywords.
Okay.
So let's see this first example.
In this example we're going to check the age of a person whether that person is an adult or minor.
Okay.
So to check that we have a right a condition here by using the if else keyword.
Okay.
So to write the conditions you need to first type the keyword.
If then inside the square brackets you need to write your condition.
Then you need to use the semicolon here to close that square bracket.
Then you need to use this then keyword.
Okay.
And after that you need to write your commands which you want to execute when this condition becomes
true.
Okay.
And when this condition becomes false then the next command okay, which is present after this else
keyword will be executed.
Okay.
And this Fi stands for the end of if else block or if else condition.
Okay, you need to use this to show that it is the end.
Okay, so this code will check the edge which is entered via keyboard.
Okay.
So now let's understand how this condition works.
Okay.
So in this condition we have used the variable age okay.
Which is defined here.
Okay.
We're gonna read the age of the person from keyboard.
Okay.
That value is assigned to this variable.
And we're gonna access that variable here.
Okay.
And we're going to compare that variable with the number 18.
Okay.
With the help of this relational operator GE hyphen GE stands for greater than or equal to.
Okay let's scroll up.
And here you can see the meaning of this relational operator is greater than or equal to.
When you use this operator in this if block like this, then this will check the value of h.
Okay.
So in such way we can write different conditions.
Okay.
According to our program needs.
So I hope you understand how this operator works.
Okay, so suppose the person entered the number 12.
Then 12 is less than 18.
So the shell will skip this part because the condition here becomes false and will execute the else
block.
Okay.
And if the person enters number 44 then in that case this condition becomes true and this part will
be executed.
Okay.
So this is how this condition works.
So let's see it practically in our shell.
So to save our time I have already typed the examples here.
Okay, so let's execute this script here.
If else dot dash.
Okay.
Now it is asking to enter the age.
So let's enter the age 12.
Okay.
It says you are a minor.
Again execute this script.
Now this time enter the age 55 and it says you are an adult.
Okay.
You can check the script here with cat if else dot bash.
Okay.
This is the same example present in our theoretical part.
Okay.
So this is how the script works.
Okay.
First it receives the value from keyboard.
Then check the value with the help of this relational operator with the number 18 and execute the command
based upon the condition.
Become true or false.
Okay.
After this, the next control statement is if Elif else.
With the help of this, you can write multiple conditions or you can handle multiple conditions.
So let's understand how this script works.
First, it receives a number from keyboard, and here that number is tested with zero okay.
Here hyphen GT stands for greater than.
So if the number is greater than zero that means the number is positive okay.
And if the number is less than zero then the number is negative.
And if you enter zero then it will print this third block.
Okay.
So this example written here is to check whether the entered number is positive or negative or zero
okay.
After this the next type of control statement is case statement.
With case statements you can match variable against multiple patterns.
Okay.
So in this example we're going to check whether the entered number is lowercase or uppercase, or a
digit, or a special character.
Okay, so first of all, it will receive the letter from keyboard okay to the variable letter.
And here we have used the keyword case okay.
And after that we are going to access the variable letter.
And after that the in keyword will create these patterns.
And we can check those patterns one by one okay.
So let's say we have entered small a from keyboard.
Then here the first pattern will be matched.
And it will print the command and shows us that you entered lowercase letter.
Okay.
As you can see here we have set the condition here a hyphen z inside this square bracket.
And this parenthesis is the closing of our first pattern okay.
First pattern of this case okay.
You can write multiple patterns, and these two semicolons are used to close the first block.
Okay.
After this the next pattern is capital A to Z.
Okay.
This pattern is used to match against capital letters.
Okay.
If this condition becomes true, then this command will be printed.
If you enter a number, then this command will be printed.
And if you enter any other value.
That means if you enter a special character, then this star pattern will be printed.
So it is called as the default case.
Okay.
The default pattern is printed only when the rest patterns doesn't match the variable, and it is the
end of this case statement.
Okay.
Esac.
It is very important to write it down.
Okay.
So let's see this example practically.
Now let's execute our script.
Case dot Don't bash.
Spelling mistake is not bash.
Okay, now it is asking to enter the letter.
So let's enter capital A, it says you entered capital letter.
Let's enter a letter capital E it says you entered lower letter.
Let's enter number four.
You entered a digit and let's enter a hash.
You enter a special character okay.
So this is how this code works okay.
You can match your variable against multiple patterns okay.
In this way.
After this let's see the next control statement that is loop with loops.
We can iterate over list of items okay.
So first of all let's see the for loop.
So with the keyword for a variable.
I is created here in this example and with in keyword the list of items okay list of these five items
is created.
Okay?
And when the shell goes to this keyword do it is ready to execute this command echo okay.
The echo will print each item in the list one by one until it reaches to the end of the items in the
list.
Okay, all these numbers from one, two, three, four, five are called as the items in the list.
Okay, which is get created with the help of this in keyword okay.
And this done is used to set the boundary line okay.
The boundary line of our for loop.
It is the end.
It doesn't go to the next line of code.
It goes back to the item list.
Okay.
So let's see how this works practically.
Okay.
So let's execute our example for bash.
And as you can see it executes the number one by one up to five.
So this is called as the loop.
It will continue to execute until the last item in the list.
Okay.
So the same way the while loop works.
The while loop executes the commands as long as the condition is true.
Okay, you need to set the condition here.
Okay.
When the condition becomes false, the shell exits this loop.
Okay.
So in this example counter variable is get created and value one is assigned.
Then we have set the condition for checking whether the entered number is less than or equal to five.
Okay.
Hyphen L stands for as you can see here less than or equal to.
Okay.
So in this example we're gonna No test or check it.
Whether the number is less than or equal to five at the beginning, the condition becomes true because
one is less than five.
So it goes to this block and print the value of variable counter to the screen.
Then it reaches to this next line.
And here the value of counter variable is increased by one because of this arithmetic operator okay.
And after performing the addition operation the result is saved to this counter variable okay.
Now the value of counter becomes two.
Then the shell reaches to this loop again.
So two is less than or equal to five okay.
Then it goes to echo and print the value of counter variable.
That is number two okay.
Again the value of counter is increased by one.
This time it becomes three.
And again it goes to the while loop.
Check it until this condition becomes false.
So when this condition becomes false, when the value becomes six, okay.
Six is not less than or equal to five.
That's why when the value of counter becomes six, the shell exits this loop.
Okay, after this, the next loop statement is until the until loop executes the commands.
As long as the condition is false, then the last part of control statement is loop control.
Here are two keywords break and continue.
The break statement exits the loop immediately or entirely.
There are some conditions where you need to exit the loop.
Okay, so in such conditions you can use the keyword break to exit the loop and continue.
Statement skips the remaining commands in the current loop iteration and moves to the next iteration.
Okay, so this is how the loop works.
You can just execute these examples and it's very easy to understand.
Okay.
And please practice the exercises.
Okay.
Practice will make you perfect.
And if you already know the computer programming then it's so easy to understand.
Okay.
After this let's talk about the last part.
And that is the function.
So functions in Linux shell scripting are powerful feature that allows you to group commands together
and execute them as a single unit okay.
That means we can group multiple commands with the help of function and execute them again and again
without writing them multiple times.
Okay, so first of all, you need to define the function.
So there is a syntax.
To define the function you need to type the function name.
Then opening closing parenthesis and then opening closing curly bracket.
And in that opening closing curly bracket you need to type your commands which you want to execute.
Or the other syntax is Axes.
First of all, you need to type the keyword function, then function name and inside opening closing
curly bracket you need to type your commands and defining the function is not enough.
Here you need to call the function.
So you just need to type your function name to call.
The function means when you call the function, then the commands which are present in our function
will be executed.
So it is very important to call the function okay.
So as you can see here this is an example greet.
So greet is a function okay.
This is the syntax which is opening and closing parenthesis.
And after that within this opening and closing curly bracket the echo command is present okay.
And here is the call of the function.
So when we call the function greet it will print this command to the computer screen.
And you can call this function many times you don't need to type this command or this code again and
again.
You just need to type it once and you can call it many times.
Okay, so let's see it practically.
Okay.
So let's see here our example function dot dash.
So as you can see here this is our function hello world which will print the message hello world.
Then my name and the message executing my first function.
And we have called this function five times.
So when I execute the script the message will be printed to the computer screen five times.
Okay so let's execute the script function dot bash.
And as you can see the message is printed here five times because we called the function five times.
Okay let's edit our script code.
So.
Function dot bash.
Okay I want to call this function only two times.
Okay.
Now let's save this script and execute it again.
And as you can see, the function is get executed only two times.
Okay.
So this is how function works.
You just need to type your commands within these opening and closing curly brackets.
And you can call them as long as you want.
Okay.
So this is how control statements and function works in Linux shell scripting.
Okay.
After this let's talk about the final part.
And that is the project.
Okay.
In the project part we are going to see the real world use of scripting.
Okay.
In this project we are going to create a real world backup script which will backup all the important
files and directories.
Okay.
Which you want.
Okay.
Uh, by reading the script, you will understand how it works.
You just need to enter the source directory and the destination directory.
Okay.