Hello, everyone, and this video, we are going to talk about improving our lineis command line, productivity,
Linux Shell scripting is a strong tool for system administrator to automate the daily repetitive tasks.
When you are running the same commands every day, you could actually automate them by putting in a
shell script.
And that's how you could increase the command line productivity.
So in this video, what we're going to learn is what is a shell shell scripting, basic shell script
input and output of shell scripts if then script statements for Loop scripts.
Show what is a show in this lesson, will cover about Shell, how the shell works and how you can run
command within the shelf, or how the environment it's set up in a shell.
So what exactly is a shell?
Shell is like a container, like, you know, you have like a Tupperware in your home or any kind of
containers that holds your environmental information inside of it.
It's interfaces between musicI and Col's or operating systems.
So you have built an operating system and then it is provided a platform you to execute commands again.
Windows is one of the platform and Windows operating system.
When you start up a GUI, you execute commands by running by double clicking on a program so that GUI
interface is your Shell and Linux.
When you don't have a GUI, you are given a terminal shell or seem like a man that is your shell.
To find your shell, you could run a command like echo dollars, dollar and zero that would tell you
what shell you are in.
And you could also get a listing of all your shells in your Linux distribution by running the command
cat at sea shells.
Your shell is also inside of ETSI password that is defined inside of the file in Windows.
GUI is a shell in Linux KATIK, which is a shell and Linux each, which is Shell, Basche, etc..
All these are the shell that is provided to a user gambo looking at this diagram again and we are second
from the top.
You'll see the application that talks to the shell and then Shell talks to Colonel.
And Col is the one that is telling hardware what to do.
Let's look at our Linux machine and we'll see how we could find our shell.
Open up a terminal real quick.
And when you do cat, so when you do echo below zero, it tells you you are in Basche, so you could
look at the available shell that is installed in your distribution by doing cat at sea.
Shells are going to tell you all the shells that are available.
And when you look at an ETSI password.
Cat ATSE password, and you'll see in my username, which is right here, it tells the shell is all
the way at the end that I have a defined show which has been Basche.
I could change the show if I want to, to a different show, but I try to keep it consistent with other
users.
So that was the shell does and will cover the later the later lessons.
How we could talk to Shell to create scripts and other things to automate our tasks.
Shell scripting.
Let's get into some fun.
Yes, everyone.
This time to get into some fun, we're going to create our own scripts, our own commands to run and
automate tasks.
So what is a shell script?
So basically a Shell script is an executable file containing multiple shell commands that are executed
sequentially.
So every time you create a shell and you create, you have a first command followed by second, third
and fourth.
Always the first command will be executed, followed by the second and third and so on.
So the file of a shell contains the first thing.
The first line is the shell itself.
Always define the shell with pound bang slash bin slash bash in Linux.
Most of the time the exclamation point is referred to as bang.
So the first line of a shell script is always start with the shell.
It's going to run in.
Most of the scripts that we will cover are all going to be Basche scripting and then the inside of a
Shell script.
We also have comments where we could tell what this command or what this script does.
Every time we put comments, we always start with hash pound sign, followed by all the comics that
we want to add.
Then we have commands, of course, what exactly you want the script to do.
And then we also have the statements which will tell the script if then while or do a for loop or all
those different conditions will we will cover one by one and those are different.
Shell scripting a shell script should have executable permission.
What it means that when you create a script, of course a script is just an empty file.
Everything in Linux is just file a flat file until you change it to an executable or link or just a
flat file.
So if you go back to the executable permissions learning that you had, you'll remember that the third
bit in in a file is always executable, which is X and on the group is X and on the others is X..
So in order for a script to run, it has to has this X bet on it.
A Shell script has to be called from the absolute path it always has.
You always have to define the complete path of the shell, meaning in this case, like home your user
directly and the name of the script.
If it is not in an absolute path, you could still run it.
If you are in the current location and you all have to do is put a dot slash and the name of your script.
Shell scripts and basic shell scripts, and that is Flon fine and nothing but the one that's where we're
going to actually do write the script and we're going to run a very small script to get you started
and you will have your own little script, only the commands that you can run.
Some of the Shell script that we Carcoar is like outputting to the screen using Eckermann.
We could create tasks such as telling what is your ID, your current locations, the files and directories
in some of your system information.
Another way we could create a script that will create your files or direct for you, and then we can
also do output to a file within your script.
And we could use some of the text processor through scripts.
And some of those source text processes are like cut or Grupp, Sorge, unique, all those different
type of commands that we run on a daily basis.
We could automate them to see, ah, to get our design result.
So I'm going to get into my Linux machine.
I'm going to find out the IP address of my machine, which is one I to 168 56 one on one.
And I'm going to log in through a PUDI session, which I already have saved.
Also type it in
one time and I could just simply log in and I could minimize this and I could focus on my PUDI session
in Pudi session.
The first thing we're going to do is create a directory in our home directory.
So are we in our home directory.
Lischak Check PWI.
It tells us we are in our home directory, so I'm going to create a directory within my home directory
and I'm going to call it my scripts and then I'm going to go into my scripts.
But first, let's verify if it created.
Yes, it did.
And then I'm going to go into my script called my scripts.
What's inside?
Of course, there's nothing inside.
I haven't read any file.
So now I want to create a first.
The first script is to create is whether you could touch a file and then V.I. it.
But since we had to write a script, we always started the I and I wanted to do I wanted to specify
the name that would be output output screen, just add or any name that you decide to pick and you had
and it will bring you to the VI mode you had I to insert.
And if it is a script you always start with hash bang slash bin bash.
This is defining that you going to run the script under Basche shell environment.
Now what do we wanted to do is rewire to display something on the screen.
So to display something on the screen you can run a command echo and then you could type Hello World.
And once you are done you could just simply escape and write.
Put out of this file and the file is created.
You could verify that it has the content that you just added by doing cat output and it tells you that
it does have all those content that you put it inside of this file.
Now, can I run this file by doing this by doing dot com slash output screen?
No, I can, because it's not even telling me that there is anything that exists like that and it's
telling me it's permission denied.
Why?
Because we do not have executable permissions to ah, output screen file.
So we have to give it executable permission to all execute output screen and now do Ellesmere's and
TR and you will see it also has changed a color and it has given executable right to everyone.
Now if you run this command you're going to see always run the command.
If you are already inside of the directory, you can run the command by dot slash and output and you
run it.
It gives you a hello world.
Hello world.
This is your first script.
And if you have this, if you have created this script, I'm telling you, you are already.
At the initial stage of being a programmer, so pat yourself in the back and you should be proud that
you could write a script and you could execute it and it works perfectly.
So this is one of the scripts that we did.
Oh, by the way, we could also run the script from Absolute Path, which is home.
I have all my scripts and output.
Now, in this case, we don't need dot slash.
We could just run the script from its absolute path and it will give us the same output.
Now I also have some of the scripts that is saved or written in my word pad.
I'm going to bring that up and I'm going to review them to tell you how we could list the commands so
I could just simply copy and paste.
So here's another script that will run commands for me.
So I'm going to do V.I. Run Commands is just a name that I'm picking for a file.
Once I do that, I hit, I insert, I go here, copy this and I'm going to paste that here.
So what it does, the first one, as you you should already know, defines which shell you are in.
You are in Bin Bash and that's where the script is going to run.
The next one tells you the description of the script.
This defines small tasks.
So I'm going to run.
A small task is just a common if I wanted to write another comment is I could do this script is written
by Emran Absol or I got to say date.
That was written in twenty eighteen.
First thing that Descript is doing is doing, who am I, who am I is a command that tells you who you
are, then it's running echo.
What Echo is doing at this point is only going to give a break inside of your script.
It's going to give you a space.
When you run this command, you're going to see the output has a space.
Then the next command is going to run is next.
Command is going to run hostname.
Next command is going to run is else minus L tr and now at the end, let's run the script, save it.
So run command what I can I run it.
Why again.
I do not have executable permissions so I'm going to assign executable permission.
I'm going to verify.
Yes.
Now I have it run command.
Yeah.
There you go.
Remember the first one was I, it tells me my username.
Which directory I'm in the sedar tree and hostname.
This is my host name and else myself.
Here are the number of files I have inside and the reason I put an echo in the middle so I could have
these spaces, the empty line, so it will be easier for me to read it.
So that's about getting the task done on our shell scripting, using our shell scripting.
Another way I'm going to call one more script is to define variables.
When you define variables, l will run those variables to whatever the values I have defined.
So I'm going to create another script called the I
Variable Command.
Again, is just the name of the file that I'm picking.
I'm going to go into insert mode and I'm going to copy this.
And I'm going to paste it inside.
OK, let's look at it, what this script is doing, the first the first line of the scripts is basically,
again, defining the show.
Second one is telling me what this script does.
Now, this one is telling me A is for Emran, B is for Absol.
C is for the next class.
Now it will Echo is my first name is Delahay.
So what is the A right here variable Emran then echo.
My surname is dollar B.
What is LRB right here is defined variable absol and the third one is my surname is seen.
Sorry I should change my surname to my class is dollar C and what is that next class.
So now let's run this command, save it.
And again don't forget to change the permissions to executable
and let's run variable command.
It says my first name is Emran, my surname is EPSO and my class is it.
Then tell me the class.
Why?
Because let's say says line six class.
This command now found.
Hmm.
What's inside of this.
Let's look at the file.
Why it didn't like it.
My class is the next class, I should say that.
But if it is not gimlets, try to get rid of these codes and then run it.
My class is still didn't run.
Let's try again.
See, my class is because since I have a space, it's not liking it.
That's why my class is Dr. C.
Let's try a single coat
and let's see if it works.
Yep, there you go.
It works.
So this double coat doesn't work.
Single coat does.
You might have to troubleshoot a little bit while you are creating your script.
So I'm glad I have this problem.
So this way you will know exactly when you when your script doesn't run, how you could troubleshoot.
You could tell by the error and you could tell by the line that it would tell you exactly your script
did not run because it has an error in line six and the line six was one, two, three, four or five,
six.
So that's how the shell scripting works.
And I really hope by now you have created three scripts and I really encourage you to go online, find
Shell Basch basic scripts that you could use and run in your environment and see how it works.
Input and output, this lesson is about writing a script where the script will wait for the user input.
So when you run the script, the script will wait for it, for you to type in something so it would
take your command and then output based on that written statement that you have given to the script.
So the way it works is that things are the the scripting commands that you need to use for that is read,
which will wait for your input and the output would be echo or whatever that you input output to the
screen using the echo come in.
So let me log into the machine and I will show you how this could be done before I confuse you more.
So I have my machine open right here and I am in my home.
I have some of my scripts directory.
I am going to create a script called Input.
Just like that, and I'm going to go ahead and hit.
And will define the variable.
Next thing as you know, you put the author, I'm not going to just wait time and date my description
and so on anyway, so you you you should type everything.
I would definitely recommend you.
So, again, since you don't have time, I really want you to learn fast.
So don't worry about it at this point.
Then you you put in like.
Echo.
Hello.
My name.
Oh, come on.
I'm having a bad time typing, OK, so hello, my name is Imran EPSO and as you know, it will just
simply display that line on the screen.
Now, what are we going to do next then we want to do?
Raino, sorry, put Echo just for my line and then Echo, what is your name?
So now we are asking user for user input and the way the script is going to wait for the user input
is by simply the command read.
The read is actual command in Unix or Linux that will wait for the user input.
Now you have to define a variable to read.
So any variable, anything that user types will be automatically added to the variable.
And what is that variable?
Well, you have to specify or define that variable.
Let's say a pick any variable.
Let's say you and I pick a name.
So we'll say, you know what, when user put in the name, put in put that input into a container,
into a variable and their variable name is actually name.
I'll just you know what.
In simple words name.
Container, so make it easy so you don't you are not confused with name name itself.
Now, once they use it, provides that information, then simply put in an empty line and then do it
go.
Hello, and now you put a dollar name.
Container.
Whatever the variable you have defined here.
You hit, enter and Echo, and that's pretty much it.
Let's give it proper permissions.
Public submissions are there.
Let's run it.
OK, you see, the first line on the screen is, hello, my name is Imran Absol or your name, and now
it's asking you what is your name?
So it's waiting.
See, you didn't get the prompt back.
Why?
Because the script is waiting for your input.
So let's say you want to type something.
Let's say Jerry Seinfeld.
And now you hit enter and it's going to say, hello, Jerry Seinfeld.
That's how the input and output script works.
Of course, it gets a little bit more complicated as we go on.
But basically, if you wanted to write a script and you wanted to take the input of the user you have
to use to read command.
Now, let's edit the same script, add a few more things in the script and make make it more professional.
Let's see the input script and.
Now, I wanted to define variable, so I would say the variable, let's say a equal hostname and, you
know, hostname is actual command and lennix that will give you the hostname of your system.
So I wanted to say hello.
My.
So her name is Darla A. And what is that, Darla A.
The Dollar A is.
The a the container right here and that container is what hostname that it will show you right here,
and then you'll say, what is your name?
And now just for giggles, I wanted to change to read it to just a different container.
I'll put B so just so clarify that, don't you do not focus on the container name.
Container name could be anything.
So you are asking what is your name.
And to the user who's going to provide the name will be put into the container called B..
Now Echo.
Hello.
B.
And by OK, so now what we are doing, we are saying is that when you run the script, tell the user,
hello, my server name is.
Dollar A and what is Dollar A Dollar is the host name, and it will be printed here and what is your
name?
And you will put in your name and it will put back to your name.
Now, before I run, I just want to tell you one thing.
There will be error in the script.
Why?
Because when you define a command with a variable, you could run that variable.
If you only simply typed already, then it will run it.
But if you try to run that variable using like an echo command, then you have to have.
This ticks on the side.
What are those ticks?
Those ticks are right below your escape key.
Those are the ticks, so you have to have those.
But anyway, I want to take it out.
For now, just so you see that, what do we get if we don't put those ticks in there?
So escape, save it.
Now let's run it.
It says, hello, my server name is Hostname.
Well, it actually took the exact name that I typed.
It didn't think that it's a command and it now is.
What is your name?
So now I'm going to put a Emran Absol and it's going to say hello, Emran.
OK, so this part from right here to here works.
This part from here to here works.
But I wanted to have my host name here.
And what is my host name?
My host name is my first Linux VM.
I wanted to have that display here.
So going back to what I was saying is if you are defining a variable and you want to run that variable
with Echo, then you have to put the little ticks.
That is right below the escape character or escape key and also below that that little curly thing.
So now let's run it and let's see if it works.
Oh, yes, it did.
You see right here, hello, my surname is this is the host name.
What is your name?
OK.
Amron Absol.
Hello, Amron Absol.
See, that's how the input and output script works, tried out with different things, play around with
it and ask questions back and forth, back and forth.
So first you should what you should do is as your practice, what you should do is you could put more
than one read.
How you could do it is like this.
You put a code just to empty line and then you do again echo what is your profession or your occupation,
what do you do?
And then you have to read here again.
Read, see.
And then.
Then you do echo again.
One more echo and now put in.
Do you like?
Now, let's put it.
What is your favorite show?
And now this time read.
A, we have USB, we have you see now use D.
OK, that's it.
Now, what do we have to do is first thing is to say is go it will.
Hello my name.
Next thing I want to do is it should echo.
You're.
Profession, which is whatever the profession sea is, or some.
OK, next one.
Dollar D, which is Fabrício is a good show.
You see how I did it first, I asked three questions, one.
To.
And three, once I have all those three questions asked.
Then I have put all these questions in different containers and I am outputting those containers one
by one by saying Hello Emran, so echo your profession, whichever is is awesome.
And then your show, whatever the show is, is a good show.
Now, let's see if we could run it successfully and put.
OK, the first name, the first question comes up, what is your name, Imron.
I see now it gave me a second question, it didn't give me the output right away.
Now it's waiting for me to give it all the information it wants.
What is your profession?
That's a.
Engineer, OK, what is your favorite show?
Sign fell.
And now and I hit enter because I have answered all the three questions and it will say, Hello, Emran
epso, your profession engineer is awesome.
Seinfeld is a good show.
See how it works.
Now, think of this.
Now, when you are installing a program or you are executing some kind of script and or even if it's
windows or even if it's Linux, now you know that when you going through the Wizard and ask you questions,
do you want to install that?
You say yes.
No, put in your organization name, put it this, you type in your name and this and that.
All the information that system gathers from you, it actually plugs that information inside wherever
your variable is defined.
So that's just a simple example of that.
So it's like your little script, a buzzard that will take all the information and it will send it out
on the screen.
You could also take that information and put it in your database, send it out in his email or whichever
the way you want.
So that's how you work with input and output scripts, if then scripts.
Now you know how to write scripts.
Now let's get into one step up, some advance scripts, some if then scripts.
These are the scripts which will define some variables.
And if they're those variables are met, then you could run those groups in.
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 it'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 a machine.
OK, witchdoctor, we are in we are in our home directly and indirectly.
Inside of it is my scripts and this directory.
We're going to create a new script and we're going to call it V.I. 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 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 what does
this script is going to look for?
Count equal hundred.
So if count equal hundred, which is the variable, then it's going to say echo the count as hundreds.
If it is not equal to 100, then it's going to say the count is not a hundred.
So that's what 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.
Yes.
What's inside of the file will look into it.
And it has exactly the same thing that we typed in.
Now we need to give executable permission to the script to 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 defined 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 wanted to do.
You could also change the output by saying if.
Then else think sorry, the count as make a lowercase count is not
100 again, count is 100.
This is a variable and it says if bracket count
equals 100, then echoed countless hundred else.
Otherwise echo sorry, the count is not a hundred and fifty.
She is the opposite of F and it is telling the script.
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.
And now when you run it, it tells you sorry, the count is not one hundred, 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 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 then new.
I'm going to define the variable which I already copied, and the first comment I have clear, it means
when the script runs is going to create the screen.
I don't want to see anything that's already on the screen.
Next thing is going to do is F 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 Aerotek flat exist, then what you should do, you should then
echo file exist, or 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 up executable permissions that's verified,
has executable permission.
OK, does.
Now let's run it if now knew, ok.
And you're right in this file does not exist.
It's telling you the actual thing.
Why?
Because there is no file exist.
There's Dukat.
If Dan and the file exist, where should the file exist?
The files should exist in my home directory error the 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 fire 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 an error message.
So this is if then 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.
I'll tell you, you are cool if it does.
Now, you said you should try.
It is just a good feel.
So I want you to run this script on your own, created create a new file, write the script and execute
it.
And I'm very sure it will run just fine without any error.
For Loop Scripts, another advanced level of scripting, you keep getting better.
My friends keep getting better.
The next script is you're going to run is for loop scripts, for loops.
What they do is it keeps running until a specified number of variables.
For example, if a variable is defined ten, then run the script 10 times or a variable is green, blue,
red.
They run the script three times for each color.
So it actually simplifies your tasks.
If you wanted to, for example, create file files or 10 files, then you don't have to run the command
again and again.
Again, full for loops will do it for you, will go back into our legs machine and will try a couple
of scripts for loops.
And then you could try on your own and you'll become proficient by running these little little Tidey
scripts.
So which directory we are in?
We are in our home directory.
Always home is not just home but entire home directory.
And the directory that we are in is my script.
So in my scripts, are we going to create a new script?
We're going to call it this time ABC do V.I., ABC.
And the first thing you know already by now is that you define the Shell script that you're going to
run this script in or the shell environment.
I would say that you're going to run the script in.
And again, I have already written a few scripts for you to go over this training.
And I'm going to go into our first group, which is a simple for loop output.
I'm going to copy the script and I'm going to just simply paste it.
This already has a defined shell, so I'm going to delete that line.
So what is it doing for I n one, two, three, four, five do so I here is a variable which is telling
that this container, which is the container name is I, it holds the numbers one, two, three, four
or five.
And then what is asking it to do.
The script is going to do echo welcome variable one times variable to time to variable three, maybe
four or five times and then once it's done it is done.
So that's why we have to define done at the end for the for loop to finish doing what is doing.
So I'm just going to save the file.
Once the file is save again, you have to give executable permission.
The permission is given.
Let's verify it.
Yes, the ABC file is there.
Let's see what's inside of it.
Inside of it is exactly has the same thing.
Let's run the script ABC and there you go.
It's doing well.
Come one times.
Two times, three times, four times and five times.
So basically if you have a repetitive task and and trust me, you're going to be working in a corporate
way, maybe you would have to do so many repetitive tasks.
And this loop script is going to become so handy for you that you will be creating scripts for every
repetitive task that you will be doing every day.
So this is outputting five times.
Let's look at the second script.
So the second script, what is doing?
It's basically doing the same thing, but this time it will just output the names.
And I'm going to make this X, Y, Z as the name of the file, and it has always starts with the shell,
the shell name has been Basche.
And now this time we're going to put in some comment and we're going to say this script.
Well, I'll put.
Output Emran with different actions because it will say, See Emran Etsy, Emran Runcie, Emran jump
and see Emran play.
So this is what this group is going to do.
So that's why I have put it in.
Just a comment.
You could put it more commonly say the script is created by Amron Absol and if you want to put a date
you can skip that up and the script starts right here.
It's telling you for I in again I is a variable in is you putting inside of your variable and variable
is just like a container.
It's telling you put eat, jump run play inside of a container called I once it has all these three
or so four words inside of the container, I then do what we are doing.
We are going see Emran dollar I.
The variable is already always be displayed with the, with the dollar and the name of the variable.
So it's telling its dollar I and then once I once the script is going to display every little, every
word in the container, then it's going to be done.
Once done, the script is created, script has done its job and we will verified the script as the next
thing you know by now is to change the permission on X, Y, Z so it will have executable permission
and execute with permission.
Once again.
I am reiterating again and again, if you can run the script, is because you do not have executable
permission.
So we do have a single executable permission.
Awesome, perfect.
Let's run it.
X, Y, Z.
Oh it says Basche, X, Y, Z.
No such file or directory Y is that.
Of course we have Putsy.
It's Z actually X, Y, Z.
So let's run it and go see Imran Etsy, Imran Runcie, Imran jump see Imran play.
So again it is doing a repetitive tasks for you.
So this for Loop is a very strong statement in a scripting environment that the lets you run repetitive
tack again and again so you don't have to do it yourself and spend time all day and creating those little
tedious tasks.
I want you to write the scripts that I have inside of this file.
This file is again in your hand outs.
I want you to download this hand out, create these little scripts again and run it and see what they
do.
It will be a great exercise for you.
The GRUP command, which stands for global regular expression print processes, text line by line and
prints any lines which match a specified pattern.
So basically, in simple words, it's a search technology or a search feature, Lennix, that allows
you to search for a specific feature or specific keyword, actually.
So not feature a specific keyword that you look for in a file or an output from a command.
So let's get into a Linux machine and I will show you a few of the commands and examples.
So this way you have a better understanding how the grep or Ekra command works.
I have my next machine right here opened and the first command we will run for Crêpe is always check
the version and check for help.
So for grep dash dash version, it will give you the version I now you, you could read it.
You know where to find the version.
The next one is grep, dash, dash, help.
This will give you all the information about the grep and its options.
If you wanted to get a quick list of all the options that are available, you always use grep, dash,
dash, help.
If you wanted to get the longer description or of every option you could use, man and do grep.
And this will give you a detailed information about the group it anyway.
So we know the version, we know how to get help.
Now let's get to the second lab question, which is to search for a keyword in a file.
So I am in my home directory.
I'll go back to the Sinophile directory in there.
I have a file called Seinfeld Characters.
I'll get it out.
And there's the contents of the Seinfeld directory.
OK, so now if I just clear the screen, go back up and I will do is I will grep for.
Sign filled in the file name, sign signed filled characters, it means only give me the lines that
matches the word Seinfeld when I hit Enter.
There are two lines that match the word Seinfeld, which is Jerry Seinfeld and Maury Seinfeld.
I could see the same way I could do with other files as well.
Let's say if I wanted to grab for my username I have and Azzi password, I could do the same thing.
Grep.
I have also I Schatz's like password and it will go into that pot and search for that specific keyword.
And it brings back one line because there is only one user in ETSI password that has the word I have
salt and that's me of course.
All right.
Next command are the next Syntex that will learn as search search for a keyword and count it.
So if I am doing, let's hit up Parakey twice and this is a comen we ran again, which is to grab Seidenfeld
from simple characters and it gives me two.
But what if you have a file that has thousands of words, that matches your criteria and you wanted
to know how many times it shows up, then you could just simply do grep minus C and it would tell you
that it is showing up two times.
So that's what minus C is used for.
The next one is minus I, which is the one that you should always use and I have a habit of using grew
up with all those minus.
Oh what minus.
I does is actually search for keyword and ignores the case sensitive meaning if I have grep Seinfeld
Seinfeld character it gave me to sign.
But what if I have an inside of this file.
I have a Seinfeld with lowercase.
So let's say if I'm grasping for Seinfeld of a lowercase s then enter.
It's not giving me anything even though I know there was a word Seinfeld in it.
So that's why I always use minus.
I option to tell my system, hey, ignore any upper case or lower case from that keyword and from that
file.
And you do once I, it actually grips and gets you the result regardless of whether they are written
in upper case or lower case.
Next one is NP displays the match lines and the line numbers.
So we used and it's clear the screen.
And if you remember we use minus C option, which gives you the total number of lines that matched our
criteria.
Now I wanted to see those lines to end in line and no where exactly it happened.
So in that case, I hit up at Parakey and replace minus seven minus and hit enter and you will see it
will give me the two lines that match my criteria.
And it also telling me the first line is actually on the line, number one, and the second match line
is line number eight.
Isn't that awesome?
Yeah, I like it.
Okay, next one is minus V also very powerful.
Meaning what if you want to get everything but that search keyword.
So let's say if I wanted to get all the lines that do not match Seidenfeld Celesta grep minus V.
Sign fell, or I could just do the I or I've to include I as in to ignore sign.
Filled from Seinfeld characters, and now you will see I have a list of every line that the whole five
content, but this does not have that to Jerry Seinfeld and Maury Seinfeld.
This these two lines are omitted or are excluded from it because I use the minus V option.
So minus V is basically telling the system, give me everything except this search keyword.
All right.
The next one is what if you wanted to combine the key word with other text processor commands that we
have learned previously, right?
Yes, we could definitely do that.
So let's say if I type grep Seinfeld or let me run the same command, the last comment grep minus V,
Seinfeld and Seinfeld characters.
Right?
Uh, right.
So what if I wanted to print from this output?
I only wanted to print the first column.
Then you could.
What you could do is you could hit up a rocky pipe at right here.
Orckit and Dahler, by the way, chop down the line.
It is not actually putting into a new line.
It's a continuation of the same line.
If you make your window bigger, you will see it is coming up in one line.
Let me show you real quick.
You see, if I do it, this C went back, so print $ one.
And.
Curly braces and cold clothes, and now it's giving me only the first column off my mashed criteria.
Now what if I wanted to get only the first three letters of this output?
Can you guess what could that be?
I'm sure you know how to do it, but if you forgot, that's fine.
You hit up Parakey now, you could put another pipe and now that you could cut minus C four characters
and you win the first three, that would be one minus three, one through three because you picking
the range and now you'll see it is giving you everything except the key word line that matches Seinfeld.
All right, let's clear the screen.
Let me make my window a little smaller and I'll move on to the next one, which is as minus L so grep
does not only search for a keyword from a file, it actually search for any keyword from any output
of a command.
So let's see if I have let me go to my home directory.
So it's home.
I have all signed.
Plus I'll go one step back to CD.
Dot will bring me back to my home level directory.
Now in here when I do X minus L, you're going to see the listing of all the files and directories.
Now what in this what if and this output, I only wanted to see the line that matched the file directory
desktop.
So I hit up arrow key, pipe it and now I'll grep it for desktop.
And let's say just for giggles, I'll use the lowercase T and hit enter and it's not going to give me
anything.
Why?
Because if you notice, the desktop has uppercase D, so you could either do hit up Arrow Key and replace
the upper lowercase to you the upper case or leave it as it and put minus high option to ignore the
case instead of hit enter and you're going to see that it is pulling only that line that matched the
word desktop.
Isn't that cool?
All right.
Now let me go into the grep and ekra functionality.
Now, you know, grep works perfectly on every keyword.
Now the ekra comes into play when you want to search two words from a file.
So let's clear the screen.
Let's go into the Seidenfeld directory.
Inside file directory, we have a file called SINTA.
Let's get that Steinfels characters file hit Enter and you'll see this.
These are the contents of that.
Well, now, what if I wanted to get the list or lines that match not only Seidenfeld, but also Castanza?
For that, we will use the egressed, which is a little powerful.
Comen then grep, so be it.
Grep minus I of course I want to ignore upper case or lower case.
Here you put double code.
The key word and the first key, what I want to say for Seinfeld.
Then for the second key word, you have to put a pipe in the middle and then put the second key word
would be chaos tends to close with the double coat and of course, specify the one name frame from where
you are trying to search.
You hit enter and now you will see it is picking up who make this big or so and hit the upper key.
So you know what exactly?
The output.
Now you see it's giving me Jerry Seinfeld, George Costanza, Frank Constanza, Estelle Costanza and
Morris.
So all the lines that match Seinfeld and Costanza's are there.
So again, if you wanted to talk it out for second column, if you wanted to cut it out for certain
characters, you could definitely do that by putting pipe after one another.