WEBVTT

00:00.200 --> 00:01.100
Hello friends.

00:01.100 --> 00:05.270
Welcome again to the part two of Fundamentals of Linux Shell Scripting.

00:05.300 --> 00:11.810
In this part we are going to talk about the control statements and functions in Linux shell scripting.

00:11.810 --> 00:20.240
But before actually moving to this video, I want you to practice the exercises which are provided after

00:20.240 --> 00:21.350
the first video.

00:21.380 --> 00:21.860
Okay.

00:21.890 --> 00:27.800
And if you have already practiced the exercises then let's move to this video.

00:27.860 --> 00:28.430
Okay.

00:29.630 --> 00:33.170
So first of all let's see what is the control statement.

00:33.200 --> 00:39.740
Control statements in Linux shell scripting are essential for creating complex and dynamic scripts.

00:39.770 --> 00:47.240
They allow you to control the flow of execution based on conditions, loops, and other criteria.

00:47.270 --> 00:50.240
Okay, we can write conditions.

00:50.270 --> 00:50.750
Okay?

00:50.780 --> 00:55.670
We can perform repetition of script with the help of loops okay.

00:55.700 --> 01:05.200
And by using conditions and loops we can control the flow of The execution of our scripts so we can

01:05.320 --> 01:13.000
write different conditions by combining different operators like string operators, Boolean operators,

01:13.060 --> 01:16.750
relational operators, arithmetic operators.

01:16.780 --> 01:17.290
Okay.

01:17.290 --> 01:20.440
And the last one is file test operators.

01:20.590 --> 01:25.480
So the control statements can be categorized into different types.

01:25.480 --> 01:27.520
So let's see the first type.

01:27.610 --> 01:30.640
The first type is conditional statements.

01:30.820 --> 01:41.320
In conditional statements we use the keyword if else if else if else to control the flow of execution

01:41.350 --> 01:42.010
okay.

01:42.010 --> 01:45.220
And we can write different conditions okay.

01:45.250 --> 01:47.800
With the help of these keywords.

01:47.830 --> 01:52.930
The if else and if else if else is called as the keywords.

01:52.960 --> 01:53.470
Okay.

01:53.500 --> 01:57.550
The meaning of these keywords is already known to our shell.

01:57.580 --> 01:58.000
Okay.

01:58.030 --> 02:03.100
So we can write different conditions by using these keywords.

02:03.130 --> 02:03.550
Okay.

02:03.580 --> 02:06.520
So let's see this first example.

02:06.580 --> 02:12.550
In this example we're going to check the age of a person whether that person is an adult or minor.

02:12.580 --> 02:13.420
Okay.

02:13.420 --> 02:20.860
So to check that we have a right a condition here by using the if else keyword.

02:20.890 --> 02:21.580
Okay.

02:21.610 --> 02:26.110
So to write the conditions you need to first type the keyword.

02:26.110 --> 02:31.600
If then inside the square brackets you need to write your condition.

02:31.600 --> 02:37.060
Then you need to use the semicolon here to close that square bracket.

02:37.060 --> 02:40.210
Then you need to use this then keyword.

02:40.300 --> 02:40.720
Okay.

02:40.750 --> 02:48.580
And after that you need to write your commands which you want to execute when this condition becomes

02:48.580 --> 02:49.270
true.

02:49.300 --> 02:50.020
Okay.

02:50.020 --> 02:58.960
And when this condition becomes false then the next command okay, which is present after this else

02:58.990 --> 03:01.270
keyword will be executed.

03:01.420 --> 03:01.900
Okay.

03:01.900 --> 03:08.760
And this Fi stands for the end of if else block or if else condition.

03:08.790 --> 03:13.530
Okay, you need to use this to show that it is the end.

03:13.560 --> 03:19.530
Okay, so this code will check the edge which is entered via keyboard.

03:19.560 --> 03:20.010
Okay.

03:20.010 --> 03:23.070
So now let's understand how this condition works.

03:23.100 --> 03:23.580
Okay.

03:23.580 --> 03:29.910
So in this condition we have used the variable age okay.

03:29.910 --> 03:31.440
Which is defined here.

03:31.470 --> 03:31.860
Okay.

03:31.890 --> 03:36.300
We're gonna read the age of the person from keyboard.

03:36.330 --> 03:36.840
Okay.

03:36.870 --> 03:39.360
That value is assigned to this variable.

03:39.360 --> 03:42.210
And we're gonna access that variable here.

03:42.240 --> 03:42.660
Okay.

03:42.690 --> 03:46.860
And we're going to compare that variable with the number 18.

03:46.980 --> 03:47.370
Okay.

03:47.400 --> 03:54.660
With the help of this relational operator GE hyphen GE stands for greater than or equal to.

03:54.690 --> 03:56.280
Okay let's scroll up.

03:57.600 --> 04:02.880
And here you can see the meaning of this relational operator is greater than or equal to.

04:02.910 --> 04:12.170
When you use this operator in this if block like this, then this will check the value of h.

04:12.200 --> 04:12.530
Okay.

04:12.560 --> 04:16.280
So in such way we can write different conditions.

04:16.310 --> 04:16.760
Okay.

04:16.790 --> 04:19.400
According to our program needs.

04:19.400 --> 04:23.720
So I hope you understand how this operator works.

04:23.750 --> 04:29.180
Okay, so suppose the person entered the number 12.

04:29.210 --> 04:31.910
Then 12 is less than 18.

04:31.910 --> 04:41.150
So the shell will skip this part because the condition here becomes false and will execute the else

04:41.150 --> 04:41.870
block.

04:41.900 --> 04:42.680
Okay.

04:42.680 --> 04:52.490
And if the person enters number 44 then in that case this condition becomes true and this part will

04:52.520 --> 04:53.780
be executed.

04:53.810 --> 04:54.170
Okay.

04:54.200 --> 04:57.500
So this is how this condition works.

04:57.500 --> 05:00.560
So let's see it practically in our shell.

05:01.790 --> 05:06.220
So to save our time I have already typed the examples here.

05:06.490 --> 05:10.450
Okay, so let's execute this script here.

05:12.100 --> 05:14.380
If else dot dash.

05:14.920 --> 05:15.430
Okay.

05:15.460 --> 05:17.740
Now it is asking to enter the age.

05:17.740 --> 05:21.100
So let's enter the age 12.

05:22.090 --> 05:22.630
Okay.

05:22.630 --> 05:24.220
It says you are a minor.

05:24.400 --> 05:26.290
Again execute this script.

05:26.320 --> 05:31.570
Now this time enter the age 55 and it says you are an adult.

05:31.600 --> 05:31.930
Okay.

05:31.960 --> 05:37.150
You can check the script here with cat if else dot bash.

05:37.180 --> 05:37.720
Okay.

05:37.750 --> 05:41.920
This is the same example present in our theoretical part.

05:41.950 --> 05:42.580
Okay.

05:42.610 --> 05:45.490
So this is how the script works.

05:45.520 --> 05:46.150
Okay.

05:46.180 --> 05:49.120
First it receives the value from keyboard.

05:49.150 --> 05:56.710
Then check the value with the help of this relational operator with the number 18 and execute the command

05:56.710 --> 05:58.840
based upon the condition.

05:58.840 --> 06:00.250
Become true or false.

06:00.280 --> 06:00.880
Okay.

06:00.910 --> 06:05.320
After this, the next control statement is if Elif else.

06:05.350 --> 06:11.620
With the help of this, you can write multiple conditions or you can handle multiple conditions.

06:11.740 --> 06:14.500
So let's understand how this script works.

06:14.530 --> 06:22.000
First, it receives a number from keyboard, and here that number is tested with zero okay.

06:22.030 --> 06:25.180
Here hyphen GT stands for greater than.

06:25.180 --> 06:30.250
So if the number is greater than zero that means the number is positive okay.

06:30.280 --> 06:35.350
And if the number is less than zero then the number is negative.

06:35.350 --> 06:40.840
And if you enter zero then it will print this third block.

06:40.870 --> 06:41.470
Okay.

06:41.470 --> 06:49.180
So this example written here is to check whether the entered number is positive or negative or zero

06:49.210 --> 06:49.720
okay.

06:49.750 --> 06:53.710
After this the next type of control statement is case statement.

06:53.710 --> 06:59.650
With case statements you can match variable against multiple patterns.

06:59.680 --> 07:00.370
Okay.

07:00.370 --> 07:07.260
So in this example we're going to check whether the entered number is lowercase or uppercase, or a

07:07.260 --> 07:09.540
digit, or a special character.

07:09.630 --> 07:16.980
Okay, so first of all, it will receive the letter from keyboard okay to the variable letter.

07:16.980 --> 07:21.330
And here we have used the keyword case okay.

07:21.360 --> 07:24.330
And after that we are going to access the variable letter.

07:24.330 --> 07:29.460
And after that the in keyword will create these patterns.

07:29.460 --> 07:33.390
And we can check those patterns one by one okay.

07:33.390 --> 07:36.660
So let's say we have entered small a from keyboard.

07:36.660 --> 07:40.800
Then here the first pattern will be matched.

07:40.800 --> 07:46.860
And it will print the command and shows us that you entered lowercase letter.

07:47.190 --> 07:47.700
Okay.

07:47.940 --> 07:55.260
As you can see here we have set the condition here a hyphen z inside this square bracket.

07:55.260 --> 08:02.490
And this parenthesis is the closing of our first pattern okay.

08:02.490 --> 08:05.430
First pattern of this case okay.

08:05.460 --> 08:12.230
You can write multiple patterns, and these two semicolons are used to close the first block.

08:12.260 --> 08:12.740
Okay.

08:12.980 --> 08:16.820
After this the next pattern is capital A to Z.

08:16.850 --> 08:17.210
Okay.

08:17.240 --> 08:21.050
This pattern is used to match against capital letters.

08:21.230 --> 08:21.410
Okay.

08:21.440 --> 08:25.940
If this condition becomes true, then this command will be printed.

08:25.970 --> 08:29.900
If you enter a number, then this command will be printed.

08:29.900 --> 08:32.900
And if you enter any other value.

08:32.930 --> 08:38.690
That means if you enter a special character, then this star pattern will be printed.

08:38.690 --> 08:41.480
So it is called as the default case.

08:41.510 --> 08:42.200
Okay.

08:42.230 --> 08:50.060
The default pattern is printed only when the rest patterns doesn't match the variable, and it is the

08:50.060 --> 08:53.480
end of this case statement.

08:53.540 --> 08:54.170
Okay.

08:54.200 --> 08:54.980
Esac.

08:55.010 --> 08:57.980
It is very important to write it down.

08:58.010 --> 08:58.610
Okay.

08:58.610 --> 09:01.190
So let's see this example practically.

09:02.450 --> 09:05.120
Now let's execute our script.

09:05.150 --> 09:06.980
Case dot Don't bash.

09:11.300 --> 09:14.510
Spelling mistake is not bash.

09:14.540 --> 09:16.730
Okay, now it is asking to enter the letter.

09:16.730 --> 09:21.530
So let's enter capital A, it says you entered capital letter.

09:21.560 --> 09:27.740
Let's enter a letter capital E it says you entered lower letter.

09:27.860 --> 09:30.740
Let's enter number four.

09:31.160 --> 09:37.790
You entered a digit and let's enter a hash.

09:38.180 --> 09:41.450
You enter a special character okay.

09:41.480 --> 09:45.110
So this is how this code works okay.

09:45.140 --> 09:50.630
You can match your variable against multiple patterns okay.

09:50.630 --> 09:51.830
In this way.

09:52.010 --> 09:58.550
After this let's see the next control statement that is loop with loops.

09:58.550 --> 10:02.540
We can iterate over list of items okay.

10:02.570 --> 10:05.750
So first of all let's see the for loop.

10:05.750 --> 10:09.370
So with the keyword for a variable.

10:09.370 --> 10:17.920
I is created here in this example and with in keyword the list of items okay list of these five items

10:17.920 --> 10:19.270
is created.

10:19.300 --> 10:19.840
Okay?

10:19.870 --> 10:28.090
And when the shell goes to this keyword do it is ready to execute this command echo okay.

10:28.120 --> 10:36.670
The echo will print each item in the list one by one until it reaches to the end of the items in the

10:36.670 --> 10:37.180
list.

10:37.210 --> 10:43.060
Okay, all these numbers from one, two, three, four, five are called as the items in the list.

10:43.090 --> 10:48.430
Okay, which is get created with the help of this in keyword okay.

10:48.460 --> 10:53.080
And this done is used to set the boundary line okay.

10:53.110 --> 10:55.870
The boundary line of our for loop.

10:55.870 --> 10:56.890
It is the end.

10:56.920 --> 10:59.620
It doesn't go to the next line of code.

10:59.650 --> 11:02.500
It goes back to the item list.

11:02.530 --> 11:03.100
Okay.

11:03.100 --> 11:06.730
So let's see how this works practically.

11:09.150 --> 11:09.600
Okay.

11:09.600 --> 11:12.990
So let's execute our example for bash.

11:14.700 --> 11:20.010
And as you can see it executes the number one by one up to five.

11:20.070 --> 11:22.860
So this is called as the loop.

11:22.860 --> 11:27.840
It will continue to execute until the last item in the list.

11:27.870 --> 11:28.650
Okay.

11:28.770 --> 11:31.290
So the same way the while loop works.

11:31.320 --> 11:35.580
The while loop executes the commands as long as the condition is true.

11:35.610 --> 11:38.010
Okay, you need to set the condition here.

11:38.040 --> 11:38.580
Okay.

11:38.610 --> 11:43.410
When the condition becomes false, the shell exits this loop.

11:43.440 --> 11:43.770
Okay.

11:43.800 --> 11:49.590
So in this example counter variable is get created and value one is assigned.

11:49.620 --> 11:57.990
Then we have set the condition for checking whether the entered number is less than or equal to five.

11:58.020 --> 11:58.560
Okay.

11:58.590 --> 12:05.610
Hyphen L stands for as you can see here less than or equal to.

12:05.640 --> 12:06.390
Okay.

12:06.390 --> 12:10.070
So in this example we're gonna No test or check it.

12:10.070 --> 12:16.550
Whether the number is less than or equal to five at the beginning, the condition becomes true because

12:16.550 --> 12:18.320
one is less than five.

12:18.350 --> 12:25.520
So it goes to this block and print the value of variable counter to the screen.

12:25.520 --> 12:28.100
Then it reaches to this next line.

12:28.100 --> 12:37.160
And here the value of counter variable is increased by one because of this arithmetic operator okay.

12:37.190 --> 12:43.490
And after performing the addition operation the result is saved to this counter variable okay.

12:43.520 --> 12:45.950
Now the value of counter becomes two.

12:45.980 --> 12:50.150
Then the shell reaches to this loop again.

12:50.150 --> 12:53.690
So two is less than or equal to five okay.

12:53.720 --> 12:57.590
Then it goes to echo and print the value of counter variable.

12:57.590 --> 12:59.840
That is number two okay.

12:59.870 --> 13:03.500
Again the value of counter is increased by one.

13:03.500 --> 13:05.450
This time it becomes three.

13:05.480 --> 13:08.270
And again it goes to the while loop.

13:08.270 --> 13:11.240
Check it until this condition becomes false.

13:11.240 --> 13:16.430
So when this condition becomes false, when the value becomes six, okay.

13:16.460 --> 13:19.910
Six is not less than or equal to five.

13:19.940 --> 13:25.190
That's why when the value of counter becomes six, the shell exits this loop.

13:25.220 --> 13:31.370
Okay, after this, the next loop statement is until the until loop executes the commands.

13:31.370 --> 13:38.270
As long as the condition is false, then the last part of control statement is loop control.

13:38.450 --> 13:41.630
Here are two keywords break and continue.

13:41.660 --> 13:46.010
The break statement exits the loop immediately or entirely.

13:46.010 --> 13:50.240
There are some conditions where you need to exit the loop.

13:50.270 --> 13:56.150
Okay, so in such conditions you can use the keyword break to exit the loop and continue.

13:56.180 --> 14:03.620
Statement skips the remaining commands in the current loop iteration and moves to the next iteration.

14:03.710 --> 14:07.670
Okay, so this is how the loop works.

14:07.700 --> 14:13.240
You can just execute these examples and it's very easy to understand.

14:13.270 --> 14:13.930
Okay.

14:13.960 --> 14:16.210
And please practice the exercises.

14:16.360 --> 14:16.660
Okay.

14:16.690 --> 14:18.580
Practice will make you perfect.

14:18.580 --> 14:23.890
And if you already know the computer programming then it's so easy to understand.

14:25.240 --> 14:25.660
Okay.

14:25.660 --> 14:27.910
After this let's talk about the last part.

14:27.910 --> 14:29.230
And that is the function.

14:29.230 --> 14:35.770
So functions in Linux shell scripting are powerful feature that allows you to group commands together

14:35.770 --> 14:39.160
and execute them as a single unit okay.

14:39.190 --> 14:46.000
That means we can group multiple commands with the help of function and execute them again and again

14:46.000 --> 14:48.670
without writing them multiple times.

14:48.700 --> 14:51.700
Okay, so first of all, you need to define the function.

14:51.700 --> 14:53.410
So there is a syntax.

14:53.410 --> 14:56.830
To define the function you need to type the function name.

14:56.830 --> 15:02.380
Then opening closing parenthesis and then opening closing curly bracket.

15:02.380 --> 15:08.680
And in that opening closing curly bracket you need to type your commands which you want to execute.

15:08.680 --> 15:10.380
Or the other syntax is Axes.

15:10.380 --> 15:17.640
First of all, you need to type the keyword function, then function name and inside opening closing

15:17.640 --> 15:23.400
curly bracket you need to type your commands and defining the function is not enough.

15:23.400 --> 15:25.590
Here you need to call the function.

15:25.590 --> 15:28.650
So you just need to type your function name to call.

15:28.650 --> 15:34.650
The function means when you call the function, then the commands which are present in our function

15:34.650 --> 15:35.880
will be executed.

15:35.880 --> 15:39.090
So it is very important to call the function okay.

15:39.090 --> 15:42.450
So as you can see here this is an example greet.

15:42.450 --> 15:44.760
So greet is a function okay.

15:44.790 --> 15:48.960
This is the syntax which is opening and closing parenthesis.

15:48.960 --> 15:54.900
And after that within this opening and closing curly bracket the echo command is present okay.

15:54.930 --> 15:57.690
And here is the call of the function.

15:57.690 --> 16:03.960
So when we call the function greet it will print this command to the computer screen.

16:03.960 --> 16:10.440
And you can call this function many times you don't need to type this command or this code again and

16:10.440 --> 16:10.800
again.

16:10.800 --> 16:15.770
You just need to type it once and you can call it many times.

16:15.800 --> 16:18.110
Okay, so let's see it practically.

16:19.610 --> 16:19.940
Okay.

16:19.940 --> 16:24.710
So let's see here our example function dot dash.

16:24.920 --> 16:31.460
So as you can see here this is our function hello world which will print the message hello world.

16:31.490 --> 16:35.960
Then my name and the message executing my first function.

16:35.960 --> 16:38.780
And we have called this function five times.

16:38.780 --> 16:44.900
So when I execute the script the message will be printed to the computer screen five times.

16:44.930 --> 16:50.420
Okay so let's execute the script function dot bash.

16:51.320 --> 16:58.130
And as you can see the message is printed here five times because we called the function five times.

16:58.160 --> 17:01.070
Okay let's edit our script code.

17:01.160 --> 17:01.940
So.

17:05.360 --> 17:06.770
Function dot bash.

17:09.380 --> 17:13.070
Okay I want to call this function only two times.

17:15.410 --> 17:15.950
Okay.

17:18.020 --> 17:22.160
Now let's save this script and execute it again.

17:22.160 --> 17:27.080
And as you can see, the function is get executed only two times.

17:27.110 --> 17:27.440
Okay.

17:27.470 --> 17:29.330
So this is how function works.

17:29.330 --> 17:34.700
You just need to type your commands within these opening and closing curly brackets.

17:34.700 --> 17:37.730
And you can call them as long as you want.

17:37.760 --> 17:38.450
Okay.

17:38.450 --> 17:43.040
So this is how control statements and function works in Linux shell scripting.

17:44.240 --> 17:44.720
Okay.

17:44.750 --> 17:46.910
After this let's talk about the final part.

17:46.910 --> 17:48.260
And that is the project.

17:48.290 --> 17:48.680
Okay.

17:48.710 --> 17:53.270
In the project part we are going to see the real world use of scripting.

17:53.300 --> 17:53.960
Okay.

17:54.140 --> 18:00.110
In this project we are going to create a real world backup script which will backup all the important

18:00.110 --> 18:01.610
files and directories.

18:01.640 --> 18:02.000
Okay.

18:02.030 --> 18:03.110
Which you want.

18:03.140 --> 18:03.800
Okay.

18:04.100 --> 18:07.520
Uh, by reading the script, you will understand how it works.

18:07.520 --> 18:12.740
You just need to enter the source directory and the destination directory.

18:12.770 --> 18:13.550
Okay.
