WEBVTT

00:01.050 --> 00:02.579
Hello and welcome to Command Line

00:02.580 --> 00:03.989
Basics. In this video, we're going

00:03.990 --> 00:05.705
to go over command loops and how we

00:05.790 --> 00:06.719
can use them.

00:06.720 --> 00:07.720
I'm Christopher.

00:07.740 --> 00:08.740
And let's begin.

00:09.630 --> 00:10.679
So we're learning objectives for

00:10.680 --> 00:12.090
this video. We're going to learn the

00:12.390 --> 00:14.007
purpose of a command loop and why

00:14.250 --> 00:15.239
we're going to use it.

00:15.240 --> 00:16.289
We're going to learn the difference

00:16.290 --> 00:18.179
between a Linux command loop and

00:18.180 --> 00:19.469
a Windows command loop.

00:20.820 --> 00:21.829
So free assessment.

00:21.840 --> 00:22.989
Why are loops useful?

00:23.340 --> 00:25.420
Is it a complete repetitive test?

00:25.860 --> 00:27.989
B, they do not overload the system.

00:28.350 --> 00:30.269
C, they will stop automatically

00:30.270 --> 00:32.280
if it's too resource intensive

00:32.460 --> 00:34.224
or D they will automatically execute

00:34.710 --> 00:35.710
new commands.

00:36.680 --> 00:38.789
The answer is a complete repetitive

00:38.790 --> 00:41.060
tasks, just like any other computer.

00:41.640 --> 00:43.799
They're used to complete

00:43.800 --> 00:45.389
repetitive tasks so we don't have to

00:45.390 --> 00:46.390
do them ourselves.

00:46.500 --> 00:48.090
So that's why in Leuser, useful,

00:48.360 --> 00:49.830
automatic, we do things for us

00:50.370 --> 00:51.595
and they can overload the

00:52.200 --> 00:54.119
system because of

00:54.120 --> 00:55.709
the nature of loops where they keep

00:55.710 --> 00:57.120
doing things over and over.

00:57.360 --> 00:59.189
So if you explicitly told it to do

00:59.190 --> 01:00.719
something and you don't give it a

01:00.730 --> 01:02.347
limit to stop, it will completely

01:03.000 --> 01:04.617
overload the system and they will

01:04.769 --> 01:06.749
not stop automatically if they

01:06.750 --> 01:08.339
are using too many resources

01:08.610 --> 01:09.959
because the computer is doing

01:09.960 --> 01:11.400
exactly what you told it to

01:11.880 --> 01:13.469
and they will not execute any

01:13.470 --> 01:14.849
command that is not a part of the

01:14.850 --> 01:15.749
loop already.

01:15.750 --> 01:17.367
So it is only doing what you tell

01:17.730 --> 01:19.319
it to, as we said already.

01:19.560 --> 01:21.324
So it will not automatically execute

01:21.660 --> 01:22.939
new commands in the loop.

01:24.910 --> 01:26.819
So the whole purpose of for

01:26.840 --> 01:28.629
and while loops is that they are

01:28.630 --> 01:30.459
going to keep going

01:30.460 --> 01:31.509
through a specific set of

01:31.510 --> 01:32.949
requirements to look through until

01:32.950 --> 01:34.420
they are met, so we can either

01:34.840 --> 01:36.699
tell it to run a specific

01:36.700 --> 01:38.890
amount of times or we can say,

01:39.070 --> 01:40.719
hey, I just want you to keep running

01:40.720 --> 01:41.847
forever and we can take

01:42.790 --> 01:44.859
it either for a file

01:45.010 --> 01:46.333
or a number so we can use a

01:46.870 --> 01:48.144
file as input if we say we

01:48.790 --> 01:49.989
have a different computer name on

01:49.990 --> 01:50.990
each line of file.

01:51.340 --> 01:53.006
We can tell it, hey, for each line

01:53.380 --> 01:55.559
in this file, I want you to ping.

01:56.080 --> 01:57.746
So it'll go through and it'll ping

01:57.790 --> 01:59.169
for every single line

01:59.920 --> 02:01.539
for those computer names inside that

02:01.540 --> 02:03.939
file and the while

02:03.940 --> 02:05.439
in the background, it will

02:06.400 --> 02:07.772
evaluate if the condition is

02:08.259 --> 02:09.909
true or not and it'll keep going

02:09.910 --> 02:11.739
until that condition is

02:12.310 --> 02:13.310
met.

02:13.630 --> 02:15.099
And it's very important to keep in

02:15.100 --> 02:16.689
mind that loops will do exactly what

02:16.690 --> 02:17.690
you tell them to.

02:18.100 --> 02:19.960
So if you say, well,

02:20.320 --> 02:21.986
while something is true and that's

02:22.150 --> 02:24.009
always true, it is always going

02:24.010 --> 02:25.270
to run and it will never stop.

02:25.630 --> 02:27.699
So that is an easy way to overload

02:27.700 --> 02:28.700
your system.

02:28.750 --> 02:30.069
So it's very important to keep in

02:30.070 --> 02:31.419
mind what you're doing and how you

02:31.420 --> 02:32.420
can prevent a for

02:33.310 --> 02:34.509
loop from going out of control.

02:35.840 --> 02:37.261
So the Linux loops will learn

02:37.870 --> 02:38.949
about these first, and they have

02:38.950 --> 02:40.419
three basic components.

02:40.780 --> 02:42.201
So we are going to define the

02:42.520 --> 02:44.049
parameter that we're going to use

02:44.050 --> 02:45.159
and the live condition.

02:45.760 --> 02:47.289
So as you can see in our example

02:47.290 --> 02:49.310
here, we are using the AI

02:49.330 --> 02:50.702
parameter and you can choose

02:50.830 --> 02:52.580
whatever letter you'd like or

02:52.600 --> 02:54.289
dislike. That's not specific.

02:54.340 --> 02:55.659
What's important is that you use the

02:55.660 --> 02:56.787
same parameter later on

02:57.580 --> 02:58.779
in the command to execute.

02:59.510 --> 03:00.969
So as you can see in the second part

03:00.970 --> 03:02.860
here, we have the AI

03:03.280 --> 03:04.479
with a dollar sign.

03:05.730 --> 03:07.050
And this is the

03:07.800 --> 03:09.659
parameter we defined already

03:09.960 --> 03:11.580
in the first part of the component

03:11.640 --> 03:12.767
for the loop and notice

03:13.590 --> 03:15.360
that they are separated by

03:15.480 --> 03:17.400
semicolons, each part of this.

03:17.880 --> 03:19.680
So we're saying for I

03:19.830 --> 03:21.389
in this list of numbers from one

03:21.390 --> 03:22.713
through five, I want you to

03:23.310 --> 03:24.659
echo I

03:25.320 --> 03:26.470
and then done.

03:27.450 --> 03:29.116
So this is a very useful way to be

03:29.340 --> 03:31.379
able to say, hey, for

03:31.380 --> 03:33.689
this specific number of steps,

03:33.690 --> 03:35.059
go ahead and execute this.

03:35.550 --> 03:37.800
So let's get into my Linux machine.

03:39.060 --> 03:40.949
And this is the command that we

03:40.950 --> 03:41.950
just had on the screen.

03:42.780 --> 03:44.669
And it does echo one through

03:44.670 --> 03:46.409
five one at a time because there's a

03:46.410 --> 03:48.119
looping through this list of numbers

03:48.120 --> 03:49.949
and doing this command for

03:49.950 --> 03:50.950
every single one.

03:51.780 --> 03:53.669
Now, if we do a for loop,

03:54.120 --> 03:56.369
let's say while this is true,

03:56.880 --> 03:58.379
I want you to echo running.

03:58.950 --> 04:01.199
And since true is always

04:01.200 --> 04:02.523
going to be true, this will

04:03.030 --> 04:04.709
keep running until we interrupted

04:04.710 --> 04:06.659
it. So while this may be funny

04:06.660 --> 04:08.424
and something exciting to know that,

04:08.610 --> 04:09.835
yes, we can do this, it's

04:10.470 --> 04:12.509
very important to stop this from

04:12.510 --> 04:13.889
running so we don't eat up all of

04:13.890 --> 04:15.930
our resources or have the wasting

04:16.800 --> 04:17.800
processing power.

04:18.570 --> 04:20.039
So it was go ahead back into my

04:20.040 --> 04:22.259
Linux machine and

04:22.260 --> 04:24.180
we had the same command, while true,

04:24.330 --> 04:26.009
do echo running

04:26.190 --> 04:27.190
done.

04:27.660 --> 04:29.579
And as we could see, it is running

04:29.580 --> 04:30.549
and it is still going.

04:30.550 --> 04:31.760
So I'm going to hit control, see,

04:31.800 --> 04:33.059
because I don't want this to keep

04:33.060 --> 04:34.060
going crazy.

04:34.590 --> 04:36.449
So that's useful to keep in mind

04:36.450 --> 04:38.165
as we're working through these four

04:38.190 --> 04:40.139
loops now, we can

04:40.140 --> 04:41.730
use a file loop as well

04:42.000 --> 04:43.421
where we can look through the

04:43.860 --> 04:45.689
file input for loop commands

04:45.690 --> 04:47.579
and then we can execute commands

04:47.580 --> 04:49.050
from that file input.

04:49.380 --> 04:50.339
And like we were saying at the

04:50.340 --> 04:51.839
beginning of this video, if we have

04:51.840 --> 04:52.920
a specific

04:53.670 --> 04:55.189
file full of computer names, we

04:55.500 --> 04:57.089
can have it ping all those computer

04:57.090 --> 04:58.609
names. Or in this example, what

04:59.040 --> 05:00.389
we're going to do is we're going to

05:00.390 --> 05:02.309
take the AI and then have

05:02.310 --> 05:04.319
it execute all of the words

05:04.320 --> 05:05.970
inside of this words that

05:06.420 --> 05:07.890
you file have an echo them

05:08.520 --> 05:10.559
and this signed cat.

05:11.630 --> 05:12.904
File, what this does is it

05:13.460 --> 05:14.460
is execute a command

05:15.350 --> 05:17.209
within the command already

05:17.210 --> 05:18.210
within this loop.

05:18.350 --> 05:20.016
So this is a very useful way to be

05:20.270 --> 05:21.593
able to get specific output

05:22.310 --> 05:24.379
and work these commands

05:24.380 --> 05:25.380
together.

05:26.390 --> 05:28.154
So for Windows Loops, what we can do

05:28.370 --> 05:30.199
is we have a few different subsets

05:30.200 --> 05:31.009
that we can use.

05:31.010 --> 05:32.959
We have these for a list of

05:32.960 --> 05:35.389
numbers and we also have this for

05:35.510 --> 05:37.039
some file input that we can use as

05:37.040 --> 05:37.819
well.

05:37.820 --> 05:39.630
So we can use a slash

05:39.710 --> 05:41.569
L for a list

05:41.570 --> 05:43.309
of numbers. And this is

05:44.120 --> 05:46.040
the starting number for one.

05:46.460 --> 05:48.049
And then the one is the amount of

05:48.050 --> 05:50.119
steps to increase for every

05:50.780 --> 05:52.460
every time we go through one of

05:52.490 --> 05:53.329
these numbers.

05:53.330 --> 05:55.459
And the 15 is the last

05:55.460 --> 05:56.499
number to end on.

05:56.900 --> 05:58.027
So this is saying, hey,

05:58.910 --> 05:59.910
for this list of

06:00.740 --> 06:02.519
numbers with the parameter G,

06:02.870 --> 06:04.579
this is the way that Windows defines

06:04.580 --> 06:06.050
parameters for loops, is using

06:06.440 --> 06:08.389
the percentage and then the

06:08.630 --> 06:09.949
letter that we can use.

06:09.950 --> 06:11.239
And this can be any letter just like

06:11.240 --> 06:13.429
from Linux in

06:13.460 --> 06:15.319
this list of numbers, I want you

06:15.320 --> 06:17.119
to echo the G parameter

06:17.480 --> 06:19.146
so it'll go through one by one and

06:19.370 --> 06:20.720
it will echo for us.

06:21.230 --> 06:22.459
So let's give a demonstration of

06:22.460 --> 06:23.389
that.

06:23.390 --> 06:24.529
So at my command prompt,

06:25.610 --> 06:27.679
I have four forward

06:27.680 --> 06:30.000
slash L percentage

06:30.350 --> 06:32.089
in this list of numbers.

06:32.090 --> 06:33.160
We're going to echo G

06:34.160 --> 06:35.899
and check it out one through all.

06:36.470 --> 06:37.470
Very useful.

06:38.040 --> 06:39.118
Now we can do the same

06:40.160 --> 06:41.839
idea with this as well,

06:42.110 --> 06:43.910
except if we don't define

06:44.480 --> 06:46.399
a list of numbers for this.

06:46.670 --> 06:48.199
Well, this is just a lower case.

06:48.200 --> 06:49.200
So it will

06:50.120 --> 06:51.329
run infinitely.

06:51.380 --> 06:52.969
It won't stop because there is no

06:52.970 --> 06:54.489
defined limits for this list to

06:54.890 --> 06:55.890
stop running.

06:56.120 --> 06:57.499
So if we go back to my command

06:57.500 --> 06:58.500
prompt and I

06:59.420 --> 07:01.249
have our four forward

07:01.250 --> 07:02.250
slash. Oh, g.

07:02.750 --> 07:04.200
In nothing.

07:04.340 --> 07:06.259
So just these empty parameters just

07:06.260 --> 07:07.260
echo loops.

07:07.670 --> 07:09.349
And it is going to keep echoing that

07:09.350 --> 07:11.419
because there is no limit set

07:11.420 --> 07:12.950
on this force is for loop.

07:14.030 --> 07:15.402
Now if we do this, if we can

07:15.920 --> 07:17.209
take file input.

07:18.230 --> 07:19.850
So let's say in this words that

07:20.570 --> 07:22.334
I want you to echo everything inside

07:22.370 --> 07:23.850
of this words file.

07:24.710 --> 07:26.139
So let's go back over.

07:27.890 --> 07:29.409
And so we have our four forward

07:29.540 --> 07:31.610
slash F the parameter G

07:31.640 --> 07:33.440
inside of this words file

07:33.530 --> 07:34.680
do echo G.

07:35.420 --> 07:37.429
And here we go. So it will echo

07:37.460 --> 07:39.109
each line separately.

07:39.650 --> 07:40.790
So very useful.

07:44.780 --> 07:46.495
All right, so post assessment, what

07:46.760 --> 07:48.619
is the purpose of the parameter

07:48.620 --> 07:49.620
in a for loop?

07:49.670 --> 07:51.499
Is it a mix of files for input?

07:51.860 --> 07:53.660
B Creed's unlimited looping,

07:54.020 --> 07:56.029
C assigns multiple variables,

07:56.300 --> 07:58.015
or D, it's used to track completion

07:58.130 --> 07:59.130
of the loop.

07:59.800 --> 08:01.639
Answer is D, the parameter is

08:01.640 --> 08:03.159
used to track completion of the

08:03.230 --> 08:05.089
loop. So this is a very

08:05.090 --> 08:06.266
useful way to take those

08:07.010 --> 08:08.899
specific parts of the loop and

08:10.670 --> 08:12.336
use it in the loop to keep looping

08:12.620 --> 08:13.819
through these different commands

08:13.820 --> 08:15.379
that we want to execute for us so we

08:15.380 --> 08:16.519
don't have to type on our own.

08:18.160 --> 08:19.569
And so in this video, we understood

08:19.570 --> 08:20.689
the purpose of a command loop.

08:20.950 --> 08:22.779
We learned the Linux loops and

08:22.780 --> 08:24.159
we learned when those loops and how

08:24.160 --> 08:25.479
we can use them for our advantage.

08:25.660 --> 08:26.949
And I hope to see you in the next

08:26.950 --> 08:27.250
video.

