[No audio]
Do While scripts. Do while scripts are
very similar to for loop scripts.
[No audio]
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, to 2pm, then you
will define that in do while script. And
a lot of times, the scripts or the
daemons, 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 2pm. As soon
as the 2pm 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.
[No audio]
Let's clear the screen, where we
are, we are in myscripts directory, which
is this directory we want to be in, and
this time we're going to create a new
script, and we are going to call it
script-dowhile.
Again, it's just the
name I'm picking, you could 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
want 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 going to take the second
one, I want you to do the first one
yourself, I'm just going to copy it, and
I'm going to 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're going to take it out by
hitting dd, and then it's telling us
that the count is zero, 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.
[No audio]
And echo, it's just an empty space, empty line,
sleep, 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, and works
perfectly. And if you want to 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 it 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, they will
stop the process and exit out of it. I
want you to create a new script in
you're in environment, and this is the
script. Again, this is also in the
handouts. You could copy and paste this
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, get
a low 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.
[No audio]