In this lesson, we'll go ahead and create a script.
So we're just going to walk through some
of the commands that's in the script
and just go over how it's broken down
and describe some of the commands
that make it easier to understand.
So the first thing we want to do we're going to create a file.
Name it check_storage.
Now we use a test command just to create
a shell of that file
and we're going to change the ownership.
So we're able to modify it
cause we're going to use a text editor.
So we going to change the ownership to me
cause it's right now it's under the root
and then we'll be able to change it
and make manipulations to it.
We'll change own command.
Change it to me.
Okay. Then we have to change the permissions.
All right.
So let's make sure they went through.
Okay. Now it's a lot easier to manipulate
the scripts using that text editor
so we going to applications, accessories
to text editor.
We're going to open it.
Typically it's going to be
in other locations
computer
scripts
storage.
Okay.
Now we're going to modify this text editor.
We want to display the line numbers
and I like to highlight the current number.
So we're going to begin the script
by writing some contextual echo statements
just to let us know
and whoever runs the command what's going on at this point.
So whenever the script first runs
this will let us know by printing out this message
on the screen saying beginning storage check.
I want to put the date in.
It's going to echo the date.
It's going to use the,
remember the dollar sign is for a variable
and going to use the date command.
Okay?
So at this point, what the system's going to do is
read date
and then it's going to print out the actual date by
running the command analytics environment.
And next we just going to add some hyphens.
Again, this isn't important
this is just going to make the output a little bit
more visually appeasing.
Okay.
So next we're going to declare a variable.
We will name it part, sample partition
do a dev
sda1
Okay. Now we're about to run a command
and we're going to declare this command.
Of course it's going to be ours.
Call it, check percentage.
This command is about to get pretty lengthy
but after I finish it, we're going to break it down.
All right.
So that's a pretty long command
but what I'm going to do is I'm going to go back to the
terminal and we're just going to break down each one
just for a little reference.
We should be familiar with all these at this point
but again, for reference, we going to break it down.
So first thing I'm going to go through is the
disk free H
so this command just shows us the storage that's available
and it's going to put in a human readable format.
That's what the H option stands for.
Okay.
Then we'll go to grip.
Now we'll have to use grip
and it's going to reference the variable that we said earlier
but in our Linux environment, it doesn't recognize part.
So we're going to have to actually type out the dev sda1.
Okay.
So what we see is just going to
select the specific partition that we're looking for
next going to add the awk command
and remember that extracts the data
in the fifth column from the results.
So, let's check that out really quick.
Okay. Since we have it up there
it's easier to see the fifth columns going to be percentages.
So it's just going to only take that value
and next we going to use the cut command
and we're going to reference the percent side
and we're going to just remove it.
So we're able to manipulate that variable only
with the number itself.
So the percentage side won't get in the way.
So it's going to be cut.
All right.
So now we only pull the number
from the, disk free command
specific to the dev sda1 partition
from the fifth column, which is the percentage
and we're going to remove the percent sign
and that's going to echo
partition was going to reference dev sda1.
We're going to reference the command that we just created.
Add the percentage sign.
Okay.
So we'll see this command is just letting us know that
we're going to spit out to the screen
or a document
that the partition dev sda1
is
and we going to run that command that we just went through
add a percent sign to that value,
which should be 25 full.
So 25% full.
Dune's going to let everyone know who's using this
that the script is complete.
Click save.
I should better run this bad boy.
Okay.
So it went through successfully.
It printed out everything that we put inside
of our check storage
that sh, echo beginning storage
echo the date, echo the device, the percent full.
We going to let the user know that
the storage check is now complete.
So now we're going to modify this script
and we are just going to
now redirect the information
to a file.
So this is a command,
that's exactly what it does.
It's the same telling us,
hey, from this point on
we're going to send everything into your home directory,
under a file called storage_report.txt
So let's also modify this last statement
to let the user know that as well.
Now last thing we must do,
we going to add this piece right here.
And that's going to let the system know
that we going to send that last echo to the screen.
Because it doesn't really need to be part of report.
That's to let the person know,
that is running the command
where the information will be stored.
Okay.
Now let's save this.
Now let's run that command one more time.
Okay.
So we see it went through.
It's letting us know that it saved to the storage report
For those of you yeah.
Okay.
Looks like it went through successfully.
Okay.
We just walked through the basics of creating
a small script of how to redirect the information to
your report file
or just keeping it on a screen
for the user to see the information,
and see the steps it's going through.
But remember the script does what you tell it to do.
So if you want to let the user know
every step that's going through,
that will be very helpful for whoever's using it.
Thank you for sticking with this walkthrough.
And I will see you in the next lesson.