In this demonstration,
we're going to return to the check storage script,
and we're going to go over a few things.
Instead of me typing everything out,
I just went ahead and typed it for us.
But we're just going to go over
conditional branches for an if else statement.
Again, we want the script to be able
to present information based
off certain conditions of the system.
So if we look here, I'll scroll
down just a little bit to get to the part
that I added.
Now, if you look at line 13,
I added our first conditional branch.
What that statement is merely saying is,
if we run the check per variable,
that run earlier,
that's the command that we created.
If the value is greater than 95
but less than 100% full,
we want to print the echo statement alert.
This partition,
which is going to be /dev/sda1, is 25%.
that's what it was in the last lesson, full
and we recommend immediate action.
But just think about that though.
The fact that it's currently
at 25% means that this wouldn't be triggered.
So the value of that number has to
actually be greater
than 95
in less than 100.
So if it's 95, it won't pass the test.
So anything 95 and below,
this alert will not be triggered, but anything 96
and above will be, long as it's less than 100.
So again, it's going to go through
each and every one of these conditional statements,
and it's going to check if,
then it's going to run the variable,
it's going to run the command,
and it's going to compare those values.
And if the statement is true
'cause remember both of the statements have to be true.
It has to be greater than 95,
but also less than 100.
And then again, it's going to go through the next one.
Else if.
Next thing it's going to do, it's going to check to see
if it's greater than 50, but less than 95.
And it's going to print out a different command.
It's going to print out caution,
consider freeing up some space.
Again, this is a more practical script
because it gives us advice
on what to do when certain conditions are met.
So again, it's going to check to see
if the particular partition is greater
than 50 and also less than 95.
The next FL statement, it's going to check to see
if the partition is less than 50%.
And if it is we're going to print out,
hey, no action is needed
because we determined anything less than 50%
is good to go.
Now, if it goes through this entire set
of conditional branches and none
of the conditions are met,
we're going to print out that
we encountered an error because it has
to meet at least one of them.
If not, then there's an issue.
Remember that the >&2 if we
have those together, that means that
we're going to print that information
out on the terminal for the user to read.
And it's going to print out an error
and it's going to give us its status code
because that's what that particular variable,
the question mark refers to.
And then we're going to exit out.
And the fi just means we're finished with that set
of conditional statements that started
with the if.
Now let's go ahead and run that command
and check storage.
Now let's view this report.
Now we see this time rather than just give us
the information, we're also giving
us the required action necessary.
It's at 25% and no action is needed,
because if you refer back to our script,
anything that's less than 50 that's
what's going to be executed.
Next we're going to go through and actually modify
this script to incorporate more than one drive
because realistically we want to know the value
of multiple sets of drives
usually all versus just one.
So we're going to modify this script
to use everything
that's associated in the /dev directory,
which points to the hard disk
that's associated to our system.
So what we need to do is modify this partition
variable that we established earlier.
So we're going to make this into a command
and this command is going to go through
and it's going to check our /dev folder
and everything that has the /dev associated
with it. That's where we're going to reference.
Let's do that really quick.
So we're going to use the variable.
So that means that we're going to get that information.
Whatever the value that is returned,
that's what we want to print out.
Okay? So we know what that command means.
We're going to take
everything from the first row
And it has to look like this /dev
and that's going to be what's returned to us.
So if we just use this command really quick
in our shell, we'll see what we return to us.
So again, we use a disk free human
readable variable.
We're going to pull out everything that's
in the first row,
and it must be presented in the /dev format.
Okay? So once we run the script
we should get the values for those two hard disks.
'Cause that's what I have associated
under /dev in this particular device.
So we should get more than one value.
We should also include the map or CentOS root.
Okay? So now we know what that command means.
Now, we need to do a loop.
We need to actually check the first one
the second one, third, etc.
For this example, we only have two
but we're going to modify this
so that it's going to check multiple.
And then it's going to convert that value
that's returned in the partition variable
into an array.
And an array is nothing more
than a set of similar values typically.
And for this example
the similar values are going to be the hard drives.
So we're going to go through
and loop through two different hard drives.
Okay, let's go ahead and modify this.
For this one do the four loop.
The i represents the individual array that's
selected once we're going through processing
each one of these commands.
We're going to do N
partition.
And this means all, every single one of them
that's what the asterisk stands for.
Okay?
This is going to begin the four loop.
And again, the i variable is the iterator.
The part variable is being referenced
as an array with the asterisk, indicating all values
in that array.
For every index in the array,
the script will execute what follows after.
Okay? So for that, we want to use the DO.
And the DO statement begins the code
that the loop will execute on each iteration.
The i is going to represent /dev map or CentOS root
and also /dev/sda1.
Now let's go ahead and go to the bottom
after the finish.
Everyone type done.
That's going to indicate the end of that loop.
So before we continue,
we have to modify every instance of
the partition variable.
Remember having return it to the i okay.
A partition here.
Need to reference that index,
not the entire array.
Okay.
Make sure there's no more associations
with the partition itself, but the index
of that partition looks like we're good to go.
Save.
Alright.
Now let's go ahead and rerun this command
and see what we get.
Okay.
Perfect.
Looks like we went through.
We checked to see the information
and the CentOS root is over 95% full
so it requires immediate action.
For the sda1 is only 25% full
so no action is needed.
Okay. We see that when it checked
multiple different hard disks,
that we were able to trigger different
conditional branches.
So we see that the CentOS root,
was above 95% so it gave us
the indication that immediate action was required.
And we see that our sda1 was still good to go
with less than 50% so no action was required.
So just a recap.
We went over how to create a script and modify it
so it's able to iterate through
multiple different
hard disks to give us more practical information
for managing our systems.
Thank you for sticking with this walkthrough
and I'll see you in the next lesson.