Accessing data from a file a file consists of some kind of data right there.
It has a lot of information inside and you need to pull that information every now and then.
Now that file could be any file that could be application file database file a simple text file a log
file or any kind of file that you try to retrieve some data massaged that data and you want to view
it the way you want to.
Now you could do that manually by running commands adding commands here and there every day.
But of course if you are doing that changing of data or running those commands every day then this task
doing in man will become tedious.
So the best thing to do is to automate that.
And the way you automated you actually create a shell script to do that.
So in this lesson we are going to create the first group of poll errors failures or warnings or any
kind of information that you want to pull from a file.
So most likely we will be using like our log messaging file which has which is a huge file which gets
it which actually logs all the error messages or warnings of failures or many other system related messages
in a file.
But sometimes when you want to view that file you don't want to go through all that stuff.
That information of stuff or something that is not related or is not actually what you're looking for.
So this is the first group that will go into and create a Scribus second script that I wanted to Khowar
is then you actually have some of the administrative commands that you run and then you want to change
the output of the file and perhaps output to a different file.
So this way every time you come in it's viewable by you and you whenever you run the command.
So let's look at our first script.
I'm going to log into my Linux machine to put the client
I'm logged in as myself and I am in my home directory.
Now I want you to copy var log messages from waar log to your current directory.
Now what is your current directory you are under is home.
I.
So I want you to go ahead and create a new directory and call it professional
scripts.
This is the name you have chosen maybe you add all our new newly created scripts into this directory.
And when I do my NSL TR you'll see at the bottom it has that directory.
Now I wanted to copy our log messages file into my current directory and that would be done.
And you hit enter.
OK you say it's coming up as permission denied.
Why.
Because our log messages file is owned by root and it is not.
It does not have a read permission for any one else.
So what we will do is we'll become root
and.
Now I will copy
waar
log messages
sorry Im doing CD or log messages to my home directory.
And from there I will copy it to professional scripts.
Now it is copied.
Now lets go to my home of all professional script territory and lets see the file is there.
OK good.
So now in order for me to read the file it should have the read permissions right here.
So what I'll do is I will go ahead and first of all change the ownership of the file.
S.H. maade Cosel messages
and belt.
OK I think it's changed my messages file to all.
Sorry I'm using changed.
It has to be changed ownership because we change the ownership
OK.
It is changed now you'll see me make this a little bigger now you will see this file messages fall is
owned by I saw.
Now I'm going to change the group as well as a change group.
I have all two messages.
And now you will see I should be able to read this file so I could have a signed read permissions right
here would change.
Or I just have given the ownership change the ownership to.
So now I could exit out of route.
Now Richard Freeman I am in slash home.
I have soul and I'm going to go into my professional scripts directory and list myself here and there
it has the messages for now to make sure I read it.
We just simply can't.
The message is just to make sure.
Agreed.
Yes I read that that's clear the screen.
Now I'm going to create a first script called pull data.
If you are using different scripting as well.
Put dot Bashur here.
Just pull the.
Now what do you do first thing.
The correct shellscript sorry the shell has just been Bash.
Next you put the off there
date
0 8 slash 0 8 slash 2020.
This script will pull only error messages from waar log messages.
You could directly pull those messages but you have to be rude.
But since BNR route is just the example that I'm giving you copied the messages file in our local and
home directory and modified.
If it is modified since we are creating for the first time to date.
So our basic things are done.
Shell is defined.
The comments are defined.
Now what are exactly we doing.
We wanted to poll only error messages from the messages file.
You don't want to view anything so to do that will have to do grep because we grepping for error messages
minus is to ignore your case or uppercase error.
From where I would always specify the absolute path you don't want to specify the relative path because
if you're running from another directory the same script will fail.
Saul professional scripts.
So for now just run that and just make sure if it works.
So controls quit right quick.
Enter now the next thing you have to do is give executable permission to pull data this myself and you
see our first script is created.
So when I run that poll data it says group home I have saw is a directory because we didn't specify
the file we need to go in one step inside.
So pull data and we're actually trying to grab for errors from directory.
So of course there's no such thing as it does have the data directory only has files.
So we'll put messages here.
Now when we run the script it is giving us only the error messages all the file all the lines inside
of that var log messaging file.
I'm only getting the error messages.
Now you're probably thinking why do I need just error messages.
Well if you are in corporate environment trust me you're going to be troubleshooting system related
application related so many times that you would have to go into the log files and look for error messages
that were failed so you could go through one line by line by line which is going to take you forever.
Or you could just simply filter out for error messages and then you will get right to the point there
why and how it is failing.
Now there are some fail messages right here of course we are not troubleshooting Leesville messages
but it is just an example that I am giving now.
Every time you come in you log into your system your manager or you're someone from the development
team or someone would say hey I'm getting error message from the system our system is running slow and
supplications failing or something.
So you just all you have to do is run the script and see what are those error messages for.
So what I will do at this point I will instead of to pull data will rename this file to move pull data
to pull errors.
So this is specific to the data.
The words that you are trying to pull.
So lets run again.
Pull errors and now we have all the errors.
Similarly if he y to pull all the warning messages we could do the same thing but instead of creating
recreating a new script what I'll do is I'll copy pull data so we pull errors and I'll name it.
Pull warning.
So now I have two scripts.
One is pull errors and the other one is pull warnings but now both are same so I have to go into the
pull site script and I'll have to instead of error put in Wong.
Now save it.
Now let's run pull.
Now and you will see you have a lot of warning signs in the system.
What's going on right here is a warning application.
Genome settings killed by signal 5 or any of the things that happen with your application or your system
will be logged here now the last one I will do is copy it again.
Pull worne.
Will do.
Pull.
Now fail not all go into failed script again instead of worne just do fail.
Now I had three scripts to verify it.
The last one is pulled a fail so let's run pull fail and now its only giving me fail related lines.
Anything that has fail in the line.
I only get that now you could also leave it as is right now if you want to but most of the time people
do is they.
I'll put this to another file so this way they could easily manipulate the text inside of that file
instead of just going into the war log messages.
So how that is done.
You could just let's go back to our first group just pull error and here will go up minus.
I er and I will output to my home directory
and this is directory.
And now I will name the output of the script as output error.
If you notice I have two slashes here so I'm going to get rid of one.
So now let's run it now this time and you run it.
It is not going to show you all those error message on the screen.
Instead it is going to push out all the messages that came with the word error.
It's going to go into a file that be just specified.
So let's run poll error.
And you say you got your problem back so it doesn't mean it didn't run.
It did run but it has created a file that you wanted to create.
Go see output error.
So now you could just simply cashed out this output Eriphyle and read through it the same way you could
do it without fail the same way you could do with warning if you wanted to.
Now also what happened is which I will also cover later on.
Many system administrators.
What they do is they schedule this type of scripts in cron job.
They tell the cron job run the script at certain time of the day and when you come in you all you have
to do is view that file.
That's one step further.
Now if you want to take it another step further what it will do is it will not only run the script it
will also send the output off the script which is the output Dasch Eriphyle through your email through
the sendmail protocol which is one of the best ways you could do that when you come in to your office
in the morning and the first thing you do open up your email and you say hey I got these emails alerts
that saying I had these typo error messages.
So this is about creating a pulling data from files.
You could also modified in terms of if you wanted to just see the number of warnings you could just
to pull warning.
So you see when you run it you get all those messages.
Now you could use your own script and combine it with operating system commands to view or to change
the output of the script.
So if you wanted to see how many warning messages or lines have been up for warning you could just do
pipe WC Maia's L and you will see there are a total of 46 warning messages.
This shows that since we have the system we have 46 warning messages that came in.
You could be more specific.
So if you want to just do grep and code and you just want to do August 27 then it will only pull all
because 27 warning messages and how many we have for August 27.
You have 46.
Basically all those messages that have came in today and you said this is how you pull data this is
how you modify the second script.
I wanted to go over as a script that will format the output of the administrative commands that do administrative
commands.
First I'm going to create a new script called admin commands
and hit enter and I will go through again because this is something that you really need to know there
date
description of the script
format the output of admin commands and the last one is modified.
Please don't get annoyed by this because I'm keep typing that.
The reason I do that is because I really want you to get in the habit of doing that.
This shows that you are an organized engineer or administrator who actually organized his or her scripts
and put all the scripts in the right way so.
OK.
Now what other commands that we want to do.
Let's do some administrative commands like date up time and D F minus H.
So let's say if I wanted to do that and let's say you run it before we run we have to give executable
permissions.
And now when we run it you will see I have the date right here up time and the deal in this case.
Now when I mean to format the output you could do that by either you run the script and format it in
a way like let's say if you only wanted to view the first column of
then you will only see like Monday the date of the up time and file systems are.
That's if you want to change the format after you run the script.
Or you could go into the script the script as the admin commands and you could go into date and you
could do awk print dollar one up time you could do awk print dollar.
That's a three
and for the D F minus H is just grep for root.
So this is how we change the output of the commands now and rerun it.
You'll see the first column it gave us Monday the second column is a date and that there is DFI F minus
H.
I just wanted to know only the root partition.
How much does space I have how much I have left.
I don't care for other file system.
So this is how you could make changes to it.
Same way there are so many commands that we learned earlier if you wanted to change the admin commands
and you want to format it.
You could try it with different commands.
And this is how basically we could access that data from file.