Today, we will continue our best scripting lessons.
Let's talk about for loops today for Loop is a programming statement in general that allows our code
to be repeatedly executed.
For instance, we run a certain Unix command five times or even 100 times.
The syntax of for loop is simple.
We start with the word for in the name of our variable.
This variable might store an incremental number that we want to use in our script, or maybe it will
store an output of some command like we did before.
It really depends on how you want to use your loop.
Let's start with a simple example and see how we can use for Loop to print a number one through 10.
In this example, we started with the word for fall by variable, which is then inside the curly braces,
we have two numbers separated by two dots.
The first number is our starting number.
The second is our last number we want to reach.
We can also add another two dots with a third number, which will be our incrementing number, but by
default it should increment by one.
After that, we read the word do Undun and between them is our code block that we want to execute.
Let's run our program.
We can also create our counter Lexy style.
Let's take an example in this example, we are using a variable name C and we initialize it with the
value of one.
Then we test it, see less than or equal five.
And finally, C++ means increment C by one.
So basically, this problem will start from one increment each time until the number is greater than
five, which is six.
And then it will stop.
Let's run the script.
Nice.
Now, let's take a practical example for those, let's spend the first five IP addresses in our twenty
four subnet.
Nice.
Can we now ping each IP with the ping command?
Let's try that.
But to make it a little faster, we will send only one packet using the Dachsie argument.
Nice, but the output is so messy.
Can we clean it a little bit as you see it from the up with the only I.P. address that replied back
is one nine to the one sixty.
That's zero that for if we look at this line, we notice that it's the only one has a string bytes from
in it.
Can we use grep to filter our output to show me the line.
Let's try.
We can't even make it cleaner by adding if statement after this fine to check of the result of this
line is true or false.
Basically, we're checking if the grip command was able to find the strength bites from or not to check
the result of the previous command.
We use the dollar sign question mark.
Let's try that.
Perfect, we we have we are still getting our output from the drip command, can we hide it?
Let's suppress it by redirecting it to DEVE No.
Much better.
Let's take a look at a different example, assuming that we have many files in our current directory.
We only want to look for and delete certain files.
Can we do that using best scripting and.
Let's try it.
In this script, we will start with full loop again, but this time we will store the output of the
command elements in our C and D variable.
Then we print this variable, which it holds the content of our directory.
Let's run it.
Nice, let's talk now for a file called Delete Me and then delete it.
That's from the command now and see if you can see this file.
Nice we were able to do with this file.
We have reached the end of this lesson.
Thank you.
And see you in the next one.