Instructor: If-then scripts.
Now you know how to write scripts.
Now let's get into the one step up,
some advanced scripts, some if-then scripts.
These are the scripts which will define some variables,
and if those variables are met,
then you could run those scripts.
An if-then script statement,
if this happens, do this.
Otherwise, do that.
So whatever that you define that has to be met,
then it will run it,
otherwise it'll give you some different output,
depending how you write a script.
To better illustrate this concept
of if-then script statements,
let's get it into our machine.
Okay, which directory we are in?
We are in our home directory,
and the directory inside of it is "myscripts."
In this directory, we're gonna create a new script,
and we're gonna call it "vi ifthen-script."
(keyboard clicking)
Again, it does not have to be the name that I'm picking.
You could pick any name,
like "myscript," or "XYZ," or "ABC."
Anything that you want.
So, when we want to write a script,
you do "vi" and the name of the file.
It'll get you into the "insert" mode.
So the first thing you do if you want to write a script
is define a variable.
(keyboard clicking)
Now, I already have a script written
that I'm gonna copy it from,
so it will save us time.
And this is a script which checks for the variable.
I'm gonna copy it, and I'm gonna paste it.
Now let's look at what this script does.
This script is gonna look for count equal 100.
So if count equal 100, which is the variable,
then it's gonna say, echo, the count is 100.
If it is not equal to 100,
then it's gonna say, the count is not 100.
So that's what pretty much if-else statement does.
So let's look at our script, let's run it.
First save it.
And once we save it,
we'll of course verify the file is created, yes.
What's inside of the file?
We'll look into it, and it has exactly the same thing
that we typed in.
Now we need to give executable permission to this script.
(keyboard clicking)
Verify it, yes it does have executable permission.
And I'm gonna run it.
(keyboard clicking)
There you go. It says the count is 100. Perfect.
Because we defined in our variable
that the count is equal to 100.
So what happens
if we change the count to,
let's say, 1?
We save it, and we run it.
Once we run it, it says "Count is not 100."
So this is the exact purpose of if-then statements,
is if...
It looks for the variable, whatever the variable is defined.
If the variable value is not met,
it will tell you exactly what you want it to do.
You could also change the output
by saying, "if, then, else,"
"Sorry, the count,"
let's make it lowercase,
"Count is not,
"100."
Again, count is 100, this is our variable,
and it says, if, bracket,
count equal 100,
then echo it, "Count is 100."
Else, otherwise, echo, "Sorry, the count is not 100."
And fi. Fi is the opposite of if,
and it is telling the script, "Now you should exit out,"
that your work is done.
An if-else statement is done.
So you exit out of it by saving it,
and now when you run it, it tells you,
"Sorry, the count is not 100."
So we just change the output to whatever you want,
and that's what the if statement...
If-else... or, if-then statement does.
Another example of if-else statement that I'm gonna pick
is right here, and I'm gonna copy it,
and I'm gonna create a new script as
"ifthen-new."
I'm gonna define the variable, which I already copied,
and the first command, I have "clear."
It means when the script runs, it's gonna clear the screen,
I don't wanna see anything that's already on the screen.
Next thing it's gonna do is if -e,
which is used, which is the option that is used,
to check if a file exists or not.
So it's saying if this file,
home/iafzal/error.txt file, exist,
then what you should do?
You should then echo "file exist."
Else, if file does not exist, then say, echo,
"file does not exist."
And that's what pretty much it's doing.
So let's run this command.
And we of course have to give executable permissions.
(keyboard clicking)
Let's verify that it has executable permission,
okay, it does.
Now let's run it.
If now. New. Okay.
When you run it, it says, "file does not exist."
It's telling you the actual thing. Why?
Because there is no file exist.
Let's do "cat."
If then,
and if the file exist, where should the file exist?
The file should exist in my home directory, error.txt.
So I'm gonna create a file now, I'm gonna just touch it,
and I'm gonna create this file.
The file is created, now I'm gonna run the script.
(keyboard clicking)
And now it says, "File does exist."
So this is sometimes a very useful command,
and it is a very useful tool for system administrators
who want it to run this command
at a certain intervals, or a certain date,
because an application creates some error files or not,
and they run this script on CronJob
to see if the file exists or not.
If it does, it means the application is running fine,
if it does not, then it means the file does not exist
and it will throw you an error message.
So this is the if-then statement,
and we do have other if-else statements examples
in this file as well, and this file
is also part of your resources, your handouts,
and I want you to try out every single script,
create every new script,
and try out different values that you have here.
So, this is a very cool script that I have written,
it tells you about your name, and it's asking you,
"Do you like working in IT?"
And it asks, if you respond yes,
it'll tell you, "You are cool,"
if it does not, it says,
"You should try IT, it's just a good field."
So I want you to run the script on your own,
create it, create a new file,
write the script, and execute it.
And I'm very sure it will run just fine without any error.