Another common construction in scripts is for and while.
Well, these are two different constructions.
While is what you use to run commands as long
as a specific condition is true.
And for is what you use to iterate
over a series of elements.
And you can use them to process all values of a variable
or to work on all files in a directory that
match a specific pattern.
Let me show you two different examples.
So let's create a script MMU, make multiple users.
We need a CBANG.
And this time, I want to include some comment as well.
MMU allows you to make multiple users.
And usernames should be provided
as command line arguments.
All right, a little bit of explanation is always good.
And I would advise, if you are going
to work on a product that's a little bit more complex,
include this explanation so that you don't lose
track of what you are doing.
Now, I'm going to use for i in $n%.
Now, what is $n%?
Well, we have seen $1.
$1 is the first positional parameter.
But if you don't know how many positional parameters are
provided, you can use $n% to refer
to all positional parameters.
And for i in $n% is iterating over all these parameters
one by one.
And in each iteration, it places the current parameter
in a variable with the name i.
First, I want to demonstrate how this is working.
So I'm using a do.
And then I'm using echo $i followed by a don.
That's as simple as it can do.
But if we have multiple parameters,
then the list should be that this script is
showing all the positional parameters in a list.
The approach that I'm using here
is also an approach that I can really recommend.
If you try to do something that's
a little bit more complex, first work out the base structure
with a simple example before you are
going to use your complex whatever you want to do.
So I'm using Schmott plus x on MMU.
And then I'm using dot slash MMU on Linda, Lisa, Lori,
and Laura.
And there we can see that that works.
So that means that at this point,
we can make the script a little bit more intelligent.
So for i in do echo user at, well, echo user at,
no, user at $i.
Not user, but user at.
I'm calling the external command user at.
And I'm going to use echo password pipe
passwd minus minus standard in on $i.
Oh, that's kind of ugly.
Echo password, what is that?
That's a hard-coded password.
But for now, we keep it as it is.
And in the iteration, we are going
to iterate over every single item in the list.
And for every single item in the list,
these two commands are going to be executed.
So first, that is user at.
And then we have passwd in the interactive way.
You know, passwd normally prints for a password.
And you need to enter it twice.
Well, here, passwd receives the password from a pipe.
And OK, the password is hard-coded.
That is ugly.
But let's just forget that for now and see if it works.
So again, MMU, Linda, Lisa, Lori.
And oh, boy, what do we get?
Well, we get bad password because the password
fills the dictionary check.
It's basically a dictionary password.
OK, well, that's too bad.
But the script proves that the iteration is working.
So let's not make it too complex,
or do make it complex if you want,
and make it very hard to guess passwords,
like p, w, caret, r, t, exclamation mark.
And then you can see that the password apparently
is not creating a problem.
But user at is because all of the users already exist.
Well, you get the idea.
This is a for iteration.
You can also do a while iteration.
Now, what I like a lot is my countdown script.
What is this countdown script doing?
Well, it is running a countdown timer.
So I am going to define counter is $1.
So I am expecting the script to work on an argument that
has been provided.
And then I'm using while true, do, done.
Let me first define the loop.
And what am I going to do while true?
Well, I'm going to use echo $counter seconds remaining
in break.
And then I'm using sleep 1.
And I'm using echo $counter minus 1.
That's a subtraction.
I need to separately show you what the subtraction is doing.
And oh, I also want to use counter
is $counter times 12.
No, times 60.
I want to use it to provide minutes as an argument.
And then I want the script to count down seconds.
Now, before showing you more detail,
let me show you this command line calculator using
echo $double parentheses 4 times 4.
And there we can see it makes a calculation.
Or 4 minus 2 makes the calculation.
And that's what I'm using in the script here.
So hopefully, that makes you understand
that we first multiply the value provided to get seconds.
And then while true means as long as the command true
can be executed successfully.
Then it is going to print how many seconds remaining
in break, sleep on second, and subtract by 1.
And the result is this, dot slash.
Hey, did you see that?
I'm using dot slash C. And that makes dot slash config.
No, it's doing that because I never made it executable.
So let's make it executable.
And then dot slash countdown with a 1.
And there you can see that it is counting down.
Oh boy, that's not going well.
Seconds remaining in break, there
is a problem in my script.
This is not what we expected it to do, right?
Yeah, you see what the problem is?
I have an echo here.
Echo counter minus 1.
That is kind of useless.
We don't need to echo.
We need to redefine the variable counter.
So counter is like we did right here.
Here we did the multiplier.
And here we are doing the subtraction.
We take off 1.
So let's run it again.
And this time, it is working as expected.
One bonus topic.
The problem right here is this.
Let me take out the multiplier to minutes
to save ourselves some time.
And now if I use countdown 5, then we
can see 5, 4, 3, 2, 1, 0.
And then what does it do?
It continues in the negative.
And maybe you don't want to do that.
Well, there's an easy fix for that.
And the easy fix is to change the while true
and make that while.
And let's do a test and make that counter minus gt,
or ge is better, greater or equal, than 0.
And now I'm running my countdown 5 again.
And then we have reached 0.
And the script stops.
Now final fine tuning.
You also want something to happen
when you have met the condition.
The script shouldn't just stop like this.
So echo time is now over.
And we should continue.
Something like that.
And that makes it a pretty decent countdown timer.
Let's verify one more time.
As you can see, that works.