Hello and welcome to Command Line
Basics. In this video, we're going
to go over command loops and how we
can use them.
I'm Christopher.
And let's begin.
So we're learning objectives for
this video. We're going to learn the
purpose of a command loop and why
we're going to use it.
We're going to learn the difference
between a Linux command loop and
a Windows command loop.
So free assessment.
Why are loops useful?
Is it a complete repetitive test?
B, they do not overload the system.
C, they will stop automatically
if it's too resource intensive
or D they will automatically execute
new commands.
The answer is a complete repetitive
tasks, just like any other computer.
They're used to complete
repetitive tasks so we don't have to
do them ourselves.
So that's why in Leuser, useful,
automatic, we 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 too many resources
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 for
and while loops is that they are
going to keep going
through a specific set of
requirements to look through until
they are met, so we can either
tell it to run a specific
amount of times or we can say,
hey, I just want you to keep running
forever and we can take
it either for a file
or a number so we can use a
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'll go through and it'll ping
for every single line
for those computer names inside that
file and the while
in the background, it will
evaluate if the condition is
true or not and it'll keep going
until that condition is
met.
And it's very important to keep in
mind that loops will do exactly what
you tell them 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.
So it's very important to keep in
mind what you're doing and how you
can prevent a for
loop from going out of control.
So the Linux loops will learn
about these first, and they have
three basic components.
So we are going to define the
parameter that we're going to use
and the live condition.
So as you can see in our example
here, we are using the AI
parameter and you can choose
whatever letter you'd like or
dislike. That's not specific.
What's important is that you use the
same parameter later on
in the command to execute.
So as you can see in the second part
here, we have the AI
with a dollar sign.
And this is the
parameter we defined already
in the first part of the component
for the loop and notice
that they are separated by
semicolons, each part of this.
So we're saying for I
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 Linux machine.
And this is the command that we
just had on the screen.
And it does echo one through
five one at a time because there's a
looping through this list of numbers
and doing this command for
every single one.
Now, if we do a for 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
it. So while this may be funny
and something exciting to know that,
yes, we can do this, it's
very important to stop this from
running so we don't eat up all of
our resources or have the wasting
processing power.
So it was go ahead back into my
Linux machine and
we had 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 going to hit control, see,
because I don't want this to keep
going crazy.
So that's useful to keep in mind
as we're working through these four
loops now, we can
use a file loop as well
where we can look through the
file input for loop commands
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
file full of computer names, we
can have it ping all those computer
names. Or in this example, what
we're going to do is we're going to
take the AI and then have
it execute all of the words
inside of this words that
you file have an echo them
and this signed cat.
File, what this does is it
is execute a command
within the command already
within this loop.
So this is a very useful way to be
able to get specific output
and work these commands
together.
So for Windows Loops, what we can do
is we have a few different subsets
that we can use.
We have these for a list of
numbers and we also have this for
some file input that we can use as
well.
So we can use a slash
L for a list
of numbers. And this is
the starting number for one.
And then the one is the amount of
steps to increase for every
every time we go through one of
these numbers.
And the 15 is the last
number to end on.
So this is saying, hey,
for this list of
numbers with the parameter G,
this is the way that Windows defines
parameters for loops, is using
the percentage and then the
letter that we can use.
And this can be any letter just like
from Linux 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.
So let's give a demonstration of
that.
So at my command prompt,
I have four forward
slash L percentage
in this list of numbers.
We're going to echo G
and check it out one through all.
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.
Well, this is just a lower case.
So it will
run infinitely.
It won't stop because there is 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. Oh, g.
In nothing.
So just these empty parameters just
echo loops.
And it is going to keep echoing that
because there is no limit set
on this force is for loop.
Now if we do this, if we can
take file input.
So let's say in this words that
I want you to echo everything inside
of this words file.
So let's go back over.
And so we have our four forward
slash F the 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 for loop?
Is it a mix of files for input?
B Creed's unlimited looping,
C assigns multiple variables,
or D, it's used to track completion
of the loop.
Answer is D, the parameter is
used to track completion of the
loop. So this is a very
useful way to take those
specific parts of the loop and
use it in the loop to keep looping
through these different commands
that we want to execute for us so we
don't have to type on our own.
And so in this video, we understood
the purpose of a command loop.
We learned the Linux loops and
we learned when those loops and how
we can use them for our advantage.
And I hope to see you in the next
video.