5.3 For and While Loops

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *

Already have an account? Sign In »

5 hours 21 minutes
Video Transcription
Hello and welcome to command line basics. In this video, we're gonna go over command loops and how we can use them on Christopher Handler and let's begin.
So we're learning objected to this video. We're gonna learn the purpose of the command. Lupin. Why? We're going to use it. We're gonna learn the difference between a Lennox Command Luke and Windows commands. Look,
it's a pre assessment wire loops. Useful. Is it a two complete repetitive tasks be they do not overload the system. See, they will stop automatically. If it's to resource intensive or D, they will automatically execute new commands.
The answer is a they complete repetitive test. Just like any other computer. They're used to complete repetitive tasks, so we don't have to do him ourselves. So that's why it looser, useful, automatically do things for us,
and they can overload the system because of the nature of loops where they keep doing things over and over. So if you explicitly told it to do something and you don't give it a limit to stop, it will completely overload the system, and they will not stop automatically if they are using to any resource is
because the computer is doing exactly what you told it to,
and they will not execute any command that is not a part of the loop already. So it is only doing what you tell it to, as we said already, so it will not automatically execute new commands in the loop.
So the whole purpose of four and while loops is that they are going to keep going through a specific set of requirements, salute through until they're met.
So we can either tell it to you run a specific amount of times or you can say, Hey, I just want you to keep running forever.
And we can take it either for, you know, file or a number, so we can use the file as input. If we say we have a different computer name on each line of file, we can tell it Hey, for each line in this file, I want you to ping
so it will go through an It'll ping for every single line for those computer names inside that file
and the while in the background, it will, you know, evaluate if the condition is true or not, and it will keep going until that condition is met,
and it's very important to keep in mind that loops will do exactly what you tell him to.
So if you say well, while something is true and that's always true, it is always going to run and it will never stop. So that is an easy way to overload your system s. So it's very important to keep in mind what you're doing and how you can prevent a four look for going out of control.
It's the limit sloops. We'll learn about these first, and they have three basic components, so we're going to define the parameter that we're going to use and the loop condition.
So as you can see in our example here, we were using the eye parameter, and you can choose what every letter you'd like over where a deal like that's not specific. What's important is that you use the same parameter later on in the command to execute.
So as we can see in the second part here, we have the eye with a dollar sign,
and this is the parameter we define already in the first part of the component for the loop
and noticed that they are separated by a semi colon. Each part of this. So we're saying four eye in this list of numbers from one through five.
I want you to echo I
and then done
so. This is a very useful way to be able to say Hey for this specific number of steps. Go ahead and execute this.
So let's get into my Lennox machine
and this is the command that we just had on the screen. And it does echo one through 51 of the time because there's a looping through this list of numbers and is doing this command for every single one.
Now, if we do a four loop, let's say, while this is true,
I want you to echo running,
and since true is always going to be true, this will keep running until we interrupted.
So while this may be funny and you know something exciting to know that yes, we can do this. It's very important the stop this from running so we don't eat up all of our resources or have it wasting, you know, processing power.
So let's go ahead back into my limits machine
and we have the same command while true Do echo running done.
And as we could see, it is running and it is still going. So I'm gonna hit control C because I don't want this to keep going crazy.
So that's useful to keep in mind. As you know, we're working through these four loops.
Now we can use a file lupas Well, where we can look through the file input for a lived command, and then we can execute commands from that file input. And like we were saying at the beginning of this video, if we have a specific, uh, file full of computer names, we can have it. Ping, all those computer names,
we're in this example. What we're gonna do is we're gonna take the eye and then have it execute
all of the words inside of this words that t x to you file and have it echoed them.
And this dollar sign cat
file. What this does is it is ah executed a command within the command already within this loop. So this is a very useful way to be able thio, get specific output and, you know, work these commands together.
So for windows loops, what we can do is we have, ah, few different subsets that we can use. We have these flash l for a list of numbers and we also have the slash f for some file input that we can use as well.
So we can use a slash l for ah, list of numbers. And this is ah,
this starting number for one. And then the one is the amount of steps to increase or every every time we go through one of these numbers and the 15 is the last number two head on. So this is saying hey, for this list of numbers with the parameter G,
this is the way that when does defines parameters for loops is using the percentage and then the
letter that we can use. This could be any later. Just like for Lennox.
Uh, in this list of numbers, I want you to echo the G parameter, so it'll go through one by one, and it will echo for us. Let's give a demonstration of that.
So at my command, prompt.
I have four forward slash l
percentage G in this list of numbers were in echo G
and check it out went through. Don't LaFours very useful.
Now we can do the same idea with this as well. Except if we don't define
a list of numbers for this slash l This is just a lower case. So, uh, it will run infinitely. It won't stop because there was no defined limits for this list to stop running.
So if we go back to my command, prompt
and I have our four forward slash l g in nothing. So just these empty parameters just echo. Oops.
And it is going to keep echoing that because there is no limit set on this force is for Luke.
No, If we do this slash f can take file input.
So let's say in this ward stopped t x t. I want you to echo everything inside of this words file.
So let's go back over.
And so we have our four ford slash f parameter g. Inside of this words file do echo g.
And here we go. So it will echo each line separately.
So very useful.
All right, So post assessment, what is the purpose of the parameter in a four loop? Is that a and marks the files for input be creates unlimited looping see assigns multiple variables or D. It's used to track completion of the loop.
Answer is D. The parameter is used to track completion off the loop. So this is a very useful way. Thio, take those specific parts of the loop and, uh, you know, use it in the loop to keep looping through these different commands that we want execute for us so we don't have to type it on our own,
right? So in this video, we understood the purpose of a command. Luke. We learned the Lenox Loops and we'll learn Windows loops and how we can use him for our advantage, and I hope to see you in the next video.
Up Next