If ten scripts now you know how to write scripts.
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 scripts.
And if that 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, I'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 direction we are in.
We are in our home directory and the directory inside of it is my scripts and this directory.
We're going to create a new script and we're going to call it by 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 that you want.
So when we want to write a script, you do Y and the name of the file, it will get you into the insert
mode.
So the first thing you do, if you want to write a script, is define a variable.
Now, I already have a script written 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 this script does.
This script is going to look for count equal 100.
So if count equals 100, which is the variable, then it's going to say echo, the count is 100.
If it is not equal to 100, then it's going to say the count is not 100.
So that's what pretty much a false statement does.
So let's look at our script.
Let's run it.
Let's 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 the script.
Verify it.
Yes, it does have executable permission, and I'm going to run it.
There you go.
It says the count is 100 perfect, because we define in our variable that the count is equal to 100.
So what happens if we change the count to, let's say, one, 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, I think.
Sorry.
The count.
Let's make a 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 140.
She is the opposite of F and it is telling the script.
Now you should exit out that your work is done.
And if lhe 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 you 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 lhe statement that I'm going to pick is right here.
I'm going to copy it.
And I'm going to create a new script is if then new.
I'm going to define the variable which I already copied.
And the first command I have clear, it means when the script runs is going to clear 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 I have error text 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 that's verified.
Has executable permission.
Okay, does.
Now let's run it if now.
Now, when you run it, it says file does not exist.
It's telling you the actual thing.
Why?
Because there is no file exist as do cat.
If then unless the file exists, where should the file exist?
The file should exist in my home directory.
Error that text.
So I'm going to create a file now.
I'm going to just touch it and I'm going to create this file.
The file is created now 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 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 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 an F ten statement and we do have other 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 you if you if you respond.
Yes, it'll tell you you are cool.
If it does not, you say you should try it.
It's just a good feel.
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.