1 00:00:00,990 --> 00:00:03,780 Today, we will continue our best scripting lessons. 2 00:00:05,190 --> 00:00:12,810 Let's talk about for loops today for Loop is a programming statement in general that allows our code 3 00:00:12,810 --> 00:00:14,710 to be repeatedly executed. 4 00:00:16,260 --> 00:00:21,540 For instance, we run a certain Unix command five times or even 100 times. 5 00:00:22,740 --> 00:00:24,870 The syntax of for loop is simple. 6 00:00:27,120 --> 00:00:30,840 We start with the word for in the name of our variable. 7 00:00:31,710 --> 00:00:37,590 This variable might store an incremental number that we want to use in our script, or maybe it will 8 00:00:37,590 --> 00:00:40,570 store an output of some command like we did before. 9 00:00:41,850 --> 00:00:44,640 It really depends on how you want to use your loop. 10 00:00:46,110 --> 00:00:51,690 Let's start with a simple example and see how we can use for Loop to print a number one through 10. 11 00:00:56,500 --> 00:01:05,770 In this example, we started with the word for fall by variable, which is then inside the curly braces, 12 00:01:05,770 --> 00:01:08,440 we have two numbers separated by two dots. 13 00:01:09,850 --> 00:01:11,920 The first number is our starting number. 14 00:01:12,200 --> 00:01:14,920 The second is our last number we want to reach. 15 00:01:16,010 --> 00:01:23,410 We can also add another two dots with a third number, which will be our incrementing number, but by 16 00:01:23,410 --> 00:01:25,480 default it should increment by one. 17 00:01:27,280 --> 00:01:34,870 After that, we read the word do Undun and between them is our code block that we want to execute. 18 00:01:36,220 --> 00:01:37,540 Let's run our program. 19 00:01:46,970 --> 00:01:50,100 We can also create our counter Lexy style. 20 00:01:50,870 --> 00:01:58,670 Let's take an example in this example, we are using a variable name C and we initialize it with the 21 00:01:58,670 --> 00:01:59,580 value of one. 22 00:02:00,500 --> 00:02:03,330 Then we test it, see less than or equal five. 23 00:02:03,350 --> 00:02:07,580 And finally, C++ means increment C by one. 24 00:02:08,600 --> 00:02:13,970 So basically, this problem will start from one increment each time until the number is greater than 25 00:02:13,970 --> 00:02:16,490 five, which is six. 26 00:02:16,970 --> 00:02:18,070 And then it will stop. 27 00:02:19,070 --> 00:02:20,240 Let's run the script. 28 00:02:26,490 --> 00:02:26,840 Nice. 29 00:02:27,600 --> 00:02:35,910 Now, let's take a practical example for those, let's spend the first five IP addresses in our twenty 30 00:02:35,910 --> 00:02:36,780 four subnet. 31 00:02:48,210 --> 00:02:48,670 Nice. 32 00:02:49,680 --> 00:02:52,420 Can we now ping each IP with the ping command? 33 00:02:53,130 --> 00:02:54,150 Let's try that. 34 00:02:55,020 --> 00:03:00,240 But to make it a little faster, we will send only one packet using the Dachsie argument. 35 00:03:29,720 --> 00:03:32,460 Nice, but the output is so messy. 36 00:03:32,720 --> 00:03:38,930 Can we clean it a little bit as you see it from the up with the only I.P. address that replied back 37 00:03:39,710 --> 00:03:41,900 is one nine to the one sixty. 38 00:03:42,440 --> 00:03:51,770 That's zero that for if we look at this line, we notice that it's the only one has a string bytes from 39 00:03:51,950 --> 00:03:52,430 in it. 40 00:03:53,330 --> 00:03:56,550 Can we use grep to filter our output to show me the line. 41 00:03:57,740 --> 00:03:58,400 Let's try. 42 00:04:32,400 --> 00:04:38,060 We can't even make it cleaner by adding if statement after this fine to check of the result of this 43 00:04:38,070 --> 00:04:39,270 line is true or false. 44 00:04:42,210 --> 00:04:50,010 Basically, we're checking if the grip command was able to find the strength bites from or not to check 45 00:04:50,010 --> 00:04:51,750 the result of the previous command. 46 00:04:51,750 --> 00:04:54,020 We use the dollar sign question mark. 47 00:04:55,200 --> 00:04:56,100 Let's try that. 48 00:05:59,570 --> 00:06:05,960 Perfect, we we have we are still getting our output from the drip command, can we hide it? 49 00:06:07,340 --> 00:06:10,910 Let's suppress it by redirecting it to DEVE No. 50 00:06:39,730 --> 00:06:40,600 Much better. 51 00:06:42,280 --> 00:06:49,150 Let's take a look at a different example, assuming that we have many files in our current directory. 52 00:06:49,720 --> 00:06:52,500 We only want to look for and delete certain files. 53 00:06:54,730 --> 00:06:56,830 Can we do that using best scripting and. 54 00:06:58,780 --> 00:06:59,610 Let's try it. 55 00:07:04,000 --> 00:07:09,160 In this script, we will start with full loop again, but this time we will store the output of the 56 00:07:09,160 --> 00:07:11,550 command elements in our C and D variable. 57 00:07:12,460 --> 00:07:16,510 Then we print this variable, which it holds the content of our directory. 58 00:07:17,530 --> 00:07:18,310 Let's run it. 59 00:07:26,130 --> 00:07:31,560 Nice, let's talk now for a file called Delete Me and then delete it. 60 00:08:16,220 --> 00:08:19,790 That's from the command now and see if you can see this file. 61 00:08:23,360 --> 00:08:25,850 Nice we were able to do with this file. 62 00:08:27,800 --> 00:08:29,420 We have reached the end of this lesson. 63 00:08:30,200 --> 00:08:30,740 Thank you. 64 00:08:30,740 --> 00:08:31,790 And see you in the next one.