It's about time now we start for loop's file system.
Well we already learn for loops earlier but now we are actually going to learn more smaller and bigger
and vast for loops as well.
I am going to actually include multiple scripts here.
Is not this lesson is not about just one script and we'll want in this lesson we are going to create
simple counting for loop scripts.
You could use it or count counting by 1 200 or whichever the specified number of times really going
to create multiple files with different names.
If you want to create let's say 10 files are you going to do touch one touch to text three.
No right.
You want to create a script create multiple files and of course put in multiple different names.
So that's where we're going to go next we're going to cover multiple create multiple files with user
input.
So user will have to specify.
It would be like hey do you want to create a false.
Yes.
And what's the name of the file.
Is how many files you want to create one to create 10 files.
What would it be then nice.
Then we have to create another script like assigned white permissions to files will create another script
assigned by permission to files with total time.
It will take.
So this is I am actually going to cover five different scripts in this lesson.
And the reason I'm keep on focusing on for loop is for loop is one of the important statement for loop
scripts that you will be using more often than any other statement scripts.
OK.
Without further ado let's log into our Linux machine.
We have our Linux machine right here.
Click the screen real quick.
I'm going to exit out and I'm going to log into my other one which is right where clear the screen.
Who am my soul.
This in my other session that was running.
I don't want to have two different sessions open.
This will do nothing but to confuse you more.
Anyway I am in my home directory and I want to go into my professional script directory that I created.
I have a few scripts that I worked on it earlier you probably have it too if you are following me.
Exactly.
Every lesson.
One by one.
Anyway let's clear this screen out.
The first thing I wanted to do is I want to keep it simple accounting script script for the for loop.
So what it does that's first create the script and you will know exactly what I'm talking about simple
count.
Again what do you have to do guys.
So now I am trying to save you time so I'm just going to leave this out there.
Date.
So you could fill that up your self description but please don't forget to do that.
I am skipping it because I don't want to waste any more time on this.
I want to get into straight into scripting.
All right so everything is devised now for loop.
It's just invariable in curly braces one dot dot that say
Tony
and.
Now what I'm saying is I want you to put all the counting from 1 to 20 into a variable called I just
say that what I'm doing.
So think of it you have a a can and you're putting 20 numbers from 1 to 20 and that can.
That.
And that can name is I you can name it anything I can name is A are just.
Even you could even name it can see and can so you could do that now.
What do you want to do now.
So you already put those numbers in a can where you want to can.
It's a perfect example so do first is at Go dollar.
So it is actually going to echo whatever is inside of the container.
So I am actually boxing 20 numbers in a box and then once I did that then I wanted to open the box and
read those numbers.
That's pretty much it.
And then I don't want to read all of them in one shot.
I want to read one number at a time.
It will read one number at a time but it will read it so fast that it will look like as if it's reading
all in one shot.
So in order for me to verify that it is reading one at a time and put a command inside called Sleep
1 Sleep 1 what is going to do is actually going to read one letter.
Sleep for one second then read the second third the fourth the fifth.
So I don't want to waste your time.
I'll just put here 10.
So we will just run the script for 10 seconds.
We'll sleep for 10 seconds.
And right here is done.
So I hope you understand this for I in put There's a container I put number 1 through 10 then what you
wanted to do was you have all the numbers in a box then do read those numbers from me please.
One two three four five six seven eight nine ten.
Perfect.
But I want you to sleep like put a break in every number while you're reading it and that break is for
only for one second.
OK.
OK sir we will do that.
Perfect.
And what do you want to do once I'm done then you're done.
Don't worry about it.
OK.
Let's go into our script modify the permissions.
Simple count.
Sorry.
And as I mentioned earlier that you could use numbering way to do the change Mada or this way I prefer
it this way so I use that.
Anyway the permissions are done.
Now let's run.
So one two three four five six seven eight nine ten done.
So it's just that simple.
Now you're probably thinking Hey Imraan What is the real life application for this type of script.
OK.
For example you probably profer questions Harkee you probably installed a lot of softwares and your
machine right.
If you are using Windows you install software it goes through a visit if you're running a Linux script
then you're installing something.
It goes through some kind of little wizard but then you also notice it gives you some kind of progress
bar.
Right.
That is where those people as developers put those progress bar by putting these counting in there.
It does the counting.
It tells you OK proximately is going to take this much time and that's how it does.
Counting one two three four five.
So that's what actually is.
Why I have not put it in a real example because I want you to go and find that out how you could put
that in a regular everyday example.
Now going to other scripts for scripts how.
I'll show you exactly where I could put in other examples as well but not just an installation wizard
but in something else.
Second script now we're doing is we're going to create multiple files right here he says create multiple
files with different names.
By the way these scripts that I'm typing Of course I do not remember all of them.
On top of my head I actually have them written in a notepad in another computer that is right sitting
next to me.
So I am not like you can say or Ron is a genius.
He remember every script on his head.
I don't I'm just like a regular person like you so I have this notepad open another computer and I have
that note pad attached to every lesson that you're going to do.
So once you're going to play that lesson or this lesson you will see an attachment that textfile likes
you to open that text file and then look at the text file and then created your script anyway.
So let's create a second one and I'm going to create that from multiple files so we
create multiple files
different names to confuse create files.
Simple as that.
After ups and insert in
Arduino goes here.
So before I do that the best way to do is just remember I usually do that so I'll copy simple count
which I created just before that and I'm going to name it a different one.
Create false are to now reply.
This way you will have everything up there that we need some Wolf this part Okay now for I I like I
so I keep picking on.
You could pick anything again.
And curly braces one through 10.
OK so now you know again I'm putting 10 numbers in my box right.
So you now you know that.
OK.
Now what what you wanted to do this time I wanted to create files and the file name should start with
Emran perfect and it should end with dollar eye.
Nice.
So you see what I'm doing it.
It is going to create 10 files and every file will have the name Imraan out one Imraan to run three
in all the way to random and see how easy it has become to create multiple files.
Probably you would have to create thousand files then creating it manually.
It's not productive at all.
So that's how we use for commands and moving forward.
So I'm going to create 10 files that the name Imraan and then I want to be done
since I copied from simple count.
It should have the execute permission as will also create false hit enter it actually threw an error
message right away.
Perfect.
I want you to have that experience as well because you will be troubleshooting error messages while
you are creating scripts as well.
So anyway it's his unexpected end of file.
It will F while looking for matching back tech and code and it's inline 7 to what I would do is go straight
into the file create files and I'll go into line 7 by putting Kallen 7 and it's coming right here.
You see it is it has this thing take I don't want it take that's why I was giving the problem now.
Now let's run it create false Bingo.
It came back when you have a command that comes back and give you a prompt back without throwing any
error is what it means.
This group ran successfully.
Now let's verify if it ran this to myself.
And you will see right here it created 10 files.
Awesome.
Perfect.
OK.
Now let's move on to the next script and next script just to copy create files.
And this time I'll do create files with and put in
in for IP I want to do so I will do.
And oh.
The create you know what will change it.
I don't want to confuse myself in out instead of that I'll do user and put
now create Faas dash user and what I'm going to do here is first Bash and let's get rid of all of that
and now I want the user to tell me how many files they want.
This is the third script.
It says right here create multiple files with user input so user the script will ask users how many
files you want to create and say how many what's the name of the file that you want to create.
So let's do that Acco how many files do you want.
And I want to on one script to read that response and that is t for toll that it's just picked.
So just put a empty line.
Then I want to throw another question at they use it and I'm going to say
please enter the star name off for us.
So the filename name if you want to apply a name to Sarva Imraan And please and to Imraan read.
And just to number not number just a variable.
Now for my and again eyes a variable.
Now I want to put sequence here put dollar
sequence 1 through t.
OK so this is going to what is going to do is this is going to put this variable which is the sequence
inside the variable i and the sequence is going to be the number of files.
So the reason I'm putting sequence here not one through a dollar or two you're probably thinking Hey
Imraan Why didn't you pick let's say dot dot like this.
This is something if you have.
Think of that then I would say you are right on the right track but when you have specif and you have
to specify the variable which is dollar t then you can use one dot dot dot and the variable because
one dot dot only works with the number or integer.
So in order for it to work you have to put a new thing called sequence 1 through that variable which
is specified by the user.
It's just that's how it is you know that's what some people say it is what it is.
OK.
Now that is done.
Do what.
My cursor is stuck.
Looks like
opes my network went down.
I don't know why.
But anyway I am going to copy this so I don't lose my work.
I'll go into my
machine.
Verify the IP and you see the IP is gone.
I don't know what happened.
But anyway I'll just restart my network
this is just a command to restart.
You could just fast forward it if you don't want.
No I just log back into my session once the session comes back up.
Now it has restarted.
Now let's see if I got an IP
Yes I got it IP and now I'm just going to do right here.
Restart session has restarted
OK.
So now I'm going to go back and to my P.S. great tree and my
script that I was working on Assayas phone Aswad POIs name this because you know I lost my session and
it still has that dots to all file open.
So it says read only at it anyway.
Rickover quit and abort.
Oh say Rickover answer yes it has recovered.
So let me just quickly save it and make sure it does save it.
It Anyway so now that's where we left off.
Now are going to do is do
and so we touch and which is the name of the file dot dollar I which is the number specified by the
user.
Done.
That's pretty much it.
Save it.
This cat
is there.
Now let's run it.
Create a false user IP.
Now it's asking me the question how many files do you want.
I want this five files please enter the start name of the files.
This time I'll just put I am artist first three initial of my name because I already have the plans
in there.
So and when you do ls mysell TR you'll see it created file files with my name and our initials and that's
how you give that option to the users.
If the users are not very friendly with the command line then you could create this group for them and
they will run it and create the script for you.
Next moving on to another script that I want to do is to assign write permission to the files.
So now you notice that I have created these files and I wanted to give let's say write permission to
every one of them.
So of course you could do that c h m o d a right and just do Imraan and then hit this asterisk and assign
it.
But what if you have different names.
Then you have to do one by one.
Or you could actually make a mistake while you're working on it so it's better to create a script to
create a script name it a sign permission.
I don't want to do that again.
Right it's all just create piles and assign permission save time assign permissions
OK.
So now for an Emraan
to
signing right permissions to dollar I which to do is execute this $2 and sleep one so we'll do one at
a time and done OK.
So basically what he's going to do is going to look for all the files that has Imraan dot asterisk and
then it's going to do echo assigning permission to all those files.
And then it's going to actually assign those permission one by one that's run it
assigning right permission to Imraan one Iran 10 Imraan 2 3 4.
How many we have.
You have 10 right.
That's the one we created in my previous script.
So let's verify it.
Yes they go so it's signed.
All right permissions.
Did it begin right or executable
we give X you.
But anyway you understand what I'm trying to do right.
So you could do instead of decks you could just put any of it moving on to our last script for for loop.
I want you to keep doing for loops or you will get used to it.
The next one I want to assign write permissions to files.
But the total time it will take.
So if it's going to take 10 seconds then I want to know that the visit to tell me he's going to take
that much time.
So I'm going to use the same script this time I'm going to just read a signed permission script I'll
come back right here and this time I'll put right here.
LS minus L No you know what.
Total let's assign a variable.
When you sign a variable you equal and then tick which is right above the tab and the variable starts
with ls minus l Emraan asterisk.
And then how many files we have.
I think this is going to give us a total number of flaws that start with Emraan.
And now it go it will take total
seconds.
To assign permissions and the rest.
I'm going to leave it as is.
I'm not changing anything.
So but this time I'm just going to change up this one.
All I did is I am telling the user that when you run this script the script will take this much time
to complete.
It's just like as if you are installing a software and the software tells you that it will take that
much time to finish the installation and I am putting creating a variable and inside the variable I'm
putting this command and that command is going to do is it's going to run at S minus l Imraan asterisk
w c minus L.
So what exactly that does.
So this commem and you do a minus.
And then you run it it will give you the total number of files that matches Imraan dot asterisk.
So basically I'm saying since we have sleep here so if there are 10 files and it's going to sleep 10
times then it's going to take 10 seconds.
So let's do a sign permission.
So you say it will take 10 seconds to assign permissions because they are 10 files.
Ok so now let's check the right permission and you see every file that has Imraan has the right permission
now so these are a few examples of creating these for loop scripts.
Try them try them again and again and you will become a very very skilled and for loop if you're trying
all of these scripts.