Instructor: Do-while scripts.
Do-while scripts are very similar to for-loop scripts.
The do-while script or statement
continually executes a block of statements
while a particular condition is true or met,
for example, if you are running a script and you create it
in a loop, it will run until a certain variable is met,
for example, if you have, if you want to run the script
until August 2015 at 2:00 PM then you will define that
and do-while script.
And a lot of times the scripts or the demons,
the process that never dies, that keep running
and running and running,
they always have do-while scripts
because they are running continuously
until a certain condition is met.
So for example, run a script until 2:00 PM,
as soon as the 2:00 PM hits, you are done, exit out,
while condition do, command1, command2, command3,
and once all these commands are done
and the condition is done, then you are done,
you are done with your script.
So let's look at our Linux machine
and let's try out a do-while script.
Let's clear the screen where we are,
we are in my script directory
which is the directory we want it to be in
and this time we're gonna create a new script
and we're gonna call it script-dowhile,
again it's just the name I'm picking,
you can pick anything you want, hit Enter,
you are in VI mode, you hit Insert and you start typing,
what's the first thing that you type?
You type hash bang bin bash
which defines your shell environment
and if you wanted to put a comment, you could say,
"This script is created by Imran Afzal on 2018,
yeah, that's fine.
And if you wanted to put a description of the script
you could do that, but let's get into the script.
I have already written down a script
and I'm just gonna go in and copy it,
there are two scripts in my handout,
I'm gonna take the second one,
I want you to do the first one yourself,
I'm just gonna copy it and I'm gonna paste it real quick.
Let's see what the script does, the script is doing,
of course, the first one is defining the shell,
which we already have,
so we gonna take it out by hitting DD.
And then it's telling us that the count is 0,
the number is 10, while the count is less than 10, do,
echo, number of seconds left to stop this process
and the process is the variable, which is $1
and echo is just an empty space, empty line.
Sleep, is a command that will wait for the number of seconds
that is defined after the sleep command.
Then it has a few number of expressions,
these are the expressions, you could get it anywhere online,
I have that expression for a while that I tried out
many times in my work environment, it works perfectly
and if you wanna use it in your work environment,
you are more than welcome to do.
So the next one is the echo
and then it says echo $1 process is stopped
and echo again,
it is telling it that it's giving an empty line,
so let's run the script and let's see what it does,
give an executable permissions.
It says 10 seconds left to stop this process,
9, 8, 7, 6, 5, 4, 3, 2, 1, and done, process is stopped,
you see that?
So these do-while scripts are created for a certain value
or certain time to be met and once that variable is met,
it will stop the process and exit out of it.
I want you to create a new script in your environment
and this is the script, again, this is also in the handouts,
you could copy and paste the script,
create a new script and run it.
You could also be my guest to go online
and just type, do-while script examples,
there are tons and tons of do-while scripts
that you could try and get a little exposure
to do-while scripts.
I'm sure by the time you will run at least five
or six scripts, you know exactly what this statement do.