For loop scripts.
Another advanced level of scripting you are.
Keep getting better, my friends keep getting better.
The next script that you're going to run is for loop scripts for loops.
What they do is it keeps running until a specified number of variables.
For example, if a variable is defined ten, then run the script ten 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 file files or ten files, then you don't have to run the command again and again
again full 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 will become proficient by running these little, little tiny, tiny scripts.
So which directory we are in?
We are in our home directory.
Always home is not just home, but entire home directory.
And the directory that we are in is my scripts.
So in my scripts we're going to create a new script, we're going to call it this time ABC to VI, ABC.
And the first thing you know already by now is that you define the Shell script that you're going to
run this script in or the shell environment.
I would say that you're going to run this script in.
And again, I have already written a few scripts for you to go over this training, and I'm going to
go into our first script, which is a simple for loop output.
I'm going to copy this script and I'm going to just simply paste it.
This already has a defined shell, so I'm going to delete that line.
So what is it doing for I?
And one, two, three, four, five do.
So I here is a variable which is telling that 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 going to do echo welcome variable one times variable
two times variable three variable for 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 for loop to finish doing what is doing.
So I'm just going to 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 this 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 and trust me, you're going to be working in a in a corporate
environment where you would have to do so many repetitive tasks and this loop script is going to become
so handy for you that you will be creating scripts for every repetitive task that you will be doing
every day.
So this is outputting five times.
Let's look at the second script.
So the second script, what is doing?
It's basically doing the same thing, but this time it will just output the names.
And I'm going to make this X, Y, Z as the name of the file.
And it has always starts with the shell.
The shell name has been Bash.
And now this time, we're going to put in some comment and we're going to say this script will output.
And I'll put Imran with different actions because it will say, See Imran.
See Imran run.
See Imran jump and see Imran play.
So this is what the script is going to do.
So that's why I have put it in.
Just a comment, you can put it more comment.
Let's say the script is created by Imran Afzal and if you want to put a date, you can skip that up
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 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 with the dollar and the name of the variable.
So it's telling it dollar I and then once I once the script is going to display every little, every
word in the container, then it's going to 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 X, Y, Z 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 execute executable permission.
Awesome.
Perfect.
Let's run it X, y, z.
Uh oh.
It says bash x, y, z.
No such file or directory.
Why is that?
Of course we have put Z.
It's Z actually X, y, Z.
So let's run it.
And here you go see Imran.
See Imran run, see Imran jump, see Imran play.
So again it is doing a repetitive tasks for you.
So this for Loop is a very strong statement in a scripting environment that will let you run repetitive
task 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 the 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.