If then scripts now you know how to write a script.
Now let's get into 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 script if their script statement.
If this happens do this or otherwise do that.
So what ever that you define There has to be met then it will run it otherwise.
Oh give you some different output depending how you write a script to better illustrate this concept
of if then scripts statements.
Let's get into our machine.
OK.
Actually we are in we are in our home directory and the directory inside of it is my script and this
directory we're going to create a new script and we call it the if then script.
Again it does not have to be the name that I'm picking you could pick any name like my script or x y
z or ABC anything with you.
So when we wanted to write a script do we.
And the name of the file would get you into the insert mode.
So the first thing you do if you want a script is define a variable.
Now I already have a script that I'm going to copy it from.
So it will save us time
and this is the script which checks for the variable.
I'm going to copy it and I'm going to paste it.
Now let's look at what's the script does
this script is going to look for cow equal hundred.
So if count equal hundred which is the variable then it's going to say echo the count hundred.
If it is not equal to 100 then it's going to say the count is not hundred.
So that's where pretty much if else statement does.
So let's look at our script let's run it that first save it and once we save it we'll of course verify
the file is created.
Here's what's inside of the file.
Look into it and it has exactly the same thing that we typed in.
Now we need to get executable permission to the script
verified.
Yes it does have executable permission and I'm going to run it.
There you go.
It says the count is hundred.
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 does make a lower case count is not a hundred.
Again count as hundred.
This is a variable.
And it says if bracket count
equal 100 then go it KOUDDOUS hundred else.
Otherwise Acco sorry the count is not hundred and fee.
She is the opposite of f..
And it is telling this group now you should exit out that your work is done and if else statement is
done so you exit out of it by saving it.
Now when you run it it tells you sorry the count is not 100.
So you just change the output to whatever you want.
And that's what the if statement if else if then statement does.
And another example of if else statement that I'm going to pick is right here.
Now I'm going to copy it.
And I'm going to create a new script is if and then you
I'm going to define the variable which I already copied.
And the first command I have clear it means when the script runs and create the screen.
I don't want to see anything that's already on the screen.
Next thing is going to do is if minus 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 no erudite text for exist then what you should do.
You should then go file exist else if file does not exist then say Echo file does not exist.
And that's what pretty much it's doing.
Let's run this command.
And we of course have to give up executable permissions.
That's why it has executable permission.
OK.
Now let's run it.
If Now knew it and you run it's this file does not exist.
It's telling you the actual thing.
Why.
Because there is no file exist as do cat then unless the file exists where should the file exist the
files should exist in my home directory error or text.
So I'm going to create a file now I'm going to just touch it and I'm going to create this for the file
is created and I'm going to run the script
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
wanted to run this command at a certain intervals or a certain day because an application creates some
error files or not and they run this script and cron job 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 tell you when an error message.
So this is as 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 hand 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 I.T. and it asks you if you
if you respond Yes I'll tell you you are cool.
If it does now you say you should try I.T. is just a good feel.
So I want you to run this script on your own created create a new file read the script and execute it.
And I'm very sure it will run just fine without any error.