Instructor: For Loop Scripts,
another advanced level of scripting.
You are keep getting better my friends.
Keep getting better.
The next script that
you're gonna run is for loop scripts.
For loops, what they do is it keeps running
until a specified number of variable.
For example, if a variable is defined 10,
then run the script 10 times.
Or variable is green, blue, red.
Then run the script three times for each color.
So it actually simplifies your tasks.
If you want to, for example, create files or 10 files
then you don't have to run the command
again and again and again.
For loops will do it for you.
We'll go back into our Linux machine
and we'll try a couple of scripts for loops,
and then you could try on your own.
And you'll become proficient by running these
little, little tiny, tiny scripts.
So, which directory we are in?
We are in our home directory.
Always home, not just home, but entire home directory.
And the directory that we are in is my scripts.
So in my scripts we are gonna create a new script.
We are gonna call it this time, abc.
Do vi, abc.
And the first thing you know already by now
is that you define the shell script
that you're gonna run this script in.
Or the shell environment I would say,
that you're gonna run this script in.
And again, I have already written few script
for you to go over this training.
And I'm gonna go into our first script.
Which is a simple for loop output.
I'm gonna copy this script,
and I'm gonna just simply paste it.
This already has a defined shell
so I'm gonna delete that line.
So what is it doing for i
in one, two, three, four, five? Do.
So i here is a variable,
which is telling this container.
Which is the container name is i
it holds the numbers one, two, three, four, five.
And then what is asking it to do?
The script is gonna do echo welcome,
variable one times variable two,
times variable three, variable four, variable five times.
And then once it's done, it is done.
So that's why we have to define done at the end,
for the four loop to finish doing what it's doing.
So I'm just gonna save the file.
Once the file is saved.
Again, you have to give executable permission.
The permission is given, let's verify it.
Yes, the abc file is there.
Let's see what's inside of it.
Inside of it is exactly has the same thing.
Let's run the script abc.
And there you go.
It's doing welcome one times, two times,
three times, four times, and five times.
So basically if you have a repetitive task,
and trust me, you're gonna be working in a
corporate environment where you would
have to do so many repetitive tasks,
and this loop script is gonna become so handy
for you that you will be creating scripts
for every repetitive task that you'll be doing every day.
So this is outputting five times.
Let's look at the second script.
So the second script, what it's doing,
it's basically doing the same thing,
but this time it will just output the names.
And I'm gonna make this xyz as the name of the file.
And it as always starts with the shell.
The shell name is bin/bash.
And now this time we're gonna put in some comment,
and we we're gonna say,
this script will output
Imran with different actions.
Because it will say, see Imran eat, see Imran run,
see Imran jump and see Imran play.
So this is what the script is gonna do.
So that's why I have put it in just a comment.
You could put it more comment.
Let's say the script
is created by Imran Abzil.
And if you wanna put a date, you can, we'll skip that out.
And the script starts right here.
It's telling you for i in,
again i is a variable,
in is you putting inside of your variable.
And variable is just like a container.
It's telling you put eat, jump, run, play
inside of a container called i.
Once it has all these three, or sorry,
four words inside of the container i, then do.
What we are doing?
We are echoing see Imran, dollar i.
the variable is already always be displayed
with the dollar and the name of the variable.
So it's telling its dollar i.
And then once the script is gonna display
every word in the container, then it's gonna be done.
Once done, the script is created.
The script has done its job.
And we will verify it.
The script is there.
Next thing you know by now,
is to change the permission on the xyz.
So it will have executable permission.
And executable permission, once again,
I am reiterating again and again
if you cannot run the script
it's because you do not have executable permission.
So we do have executable permission.
Awesome, perfect.
Let's run it. Xyz.
Uh-oh it says bash xyz, no such file or directory.
Why is that?
Of course we have put C,
it's Z actually xyz.
So let's run it.
And there you go.
See Imran ear, see Imran run,
see Imran jump, see Imran play.
So again, it is doing a repetitive task for you.
So this for loop is a very strong statement
in a scripting environment that will let you
run repetitive tasks again and again.
So you don't have to do it yourself and
spend time all day and creating those little tedious tasks.
I want you to write other scripts
that I have inside of this file.
This file is again in your handouts.
I want you to download this handout,
create these little scripts again
and run it and see what they do.
It will be a great exercise for you.