Today we're just gonna continue on with commands. So, today I have some, well,
some of them perhaps can be referred to as text editing commands, but they are
mainly for file listings, and for combining with other commands in order
to obtain better results. Anyway, I'm gonna show very soon what I mean. So
first off, we have echo. So, echo is, well, just read the English word, and think to
what that particular English word is referring to. Basically, an echo, you say
something, and then it is repeated. Okay, let's say echo "Hey there how are you"
Excellent! So it just echoed it out to the standard output, and this can be also
redirected. And I will talk a great deal about redirecting traffic from
standard output, and standard error, and such stuff. You do it basically like
this with the arrows. Let me just show you a very simplified example of this. So
let's say we want to echo this to a file, ECHO. Excellent! And now we can use the
cat command, which is ECHO, excellent! It says, Hey there how are you. That is the
contents of the echo file. But if I change this, and if I say
"Hey" > ECHO, it's gonna delete everything
in there, and it's just gonna write Hey. So be very careful with echo, what you do
and how you actually behave with it. Primarily because, you can end up messing
around with the content of other files, deleting them and substituting them with
something else. But echo is a very nice thing especially in bash scripting. It
allows you to actually be informed, it allows for that verbose output that you
are getting when you're running commands.
When the program is actually telling you, oh okay, I am doing this now.
After that I'm going to be doing this, it takes me this much time to do that, and
so on and so forth. So it's a very nice functionality, it's a very nice feature
here. Anyway, we'll just take a look at echo - -help. Be sure to take a look at...
oh, wait. No, okay, this is one of those cases where you cannot access help
like this. But, fear not. We got the man pages. What do the all-knowing
man pages say? So, look, it's not, I mean, okay, yes it's a
fairly simple command, but that does not have to mean that it doesn't have any
arguments. Just check this out. There's so much of it. Maybe not as much as in the others,
but certainly a good amount of them. So you have these escape characters, we're
gonna be learning about them quite a bit later on, how to use them. But, for
example, if you wanted to
let's say echo a quotation mark, you would need to use a backslash as an
escape character for that. Or, if you wanted to echo a backslash, you would
need to use backslash as an escape character for itself. Well,
I'll show you what I be in in a moment, but for the time being I'm just gonna
have a look at these options. Excellent! So we need -e to enable
interpretation of backslash escapes. Very, very important. I will show you why in a
moment. Okay, so -E is apparently to disable
it, which is not the best of things. Here you have the
synopsis like with any other command. But, let me just show you what I mean by this.
So, if I typed in, let's first clear the screen, if I typed in echo test,
I don't know, backslash, what did I get? The command is not done.
It's interpreting, it's offering me a chance to issue
more inputs. If I type in the quotation marks again, you see what happened there.
So first of all, let's go about this one more time. Now I have typed in here echo
as before, and I have the quotation marks, and I have the word test inside,
and in addition at the end of the word test I have an escape character. An escape
character basically tells it to interpret this and please
print it out. Do not interpret it as a part of your command, because the
quotation marks are part of the command or the argument for the echo. Basically,
they provide the border lines for the string that you are putting in here.
I suppose there isn't much point to put them now because there's just one word,
but let's say I have "how was your test" "how did you do on your test" would be a
better way, excellent! So now this wouldn't function without
quotation marks because you have spaces in between. If you don't have any spaces
in between the words, then you can do this without the quotation mark, or
should be able to anyway. But, since the quotation marks are part
of this command, the backslash tells echo that hey, this guy at
the end, this last quotation mark, is actually not a part of your standard
command. You are supposed to print this out only and not interpret it as a
command. You should interpret it as a part of this entire string. Which is of
course a bit tricky because then echo doesn't quite understand. It's just
waiting for you to give him more so it can finish. So if I press ENTER now, I
just get prompted with hey, need something else. It's expecting
something else. It stands to reason to it that you should type something else, and
quite frankly it is quite rational that you should. So I just add another
quotation mark which will match its twin at the beginning. So these two will be the
beginning and the end, because this one can't be as it was actually. There is an
escape character in front of it and it completely throws it out of the equation.
So if I press ENTER here, there we go. How did you do your test and then the
quotation mark at the end. That's pretty much the only way to print quotation
marks and some other things which would generally be considered to be part of
the command. Rather instead, you can use escape characters, such as backslash, in
order to avoid this these sort of things.
Anyway, that's why we need escape characters. That's why they're very
useful, and that is why we use them. We don't only use, I mean, you're gonna
use the backslash even though you don't need it for navigation.
Rather instead, you will use it for escaping things such as these. Perhaps
not in the echo command, but believe me there will come a time where you will
need to process a lot of things, with a lot of other programs, and you will need
them. Anyway, what I showed you a moment ago is in fact this greater
than sign. The greater than sign basically says to echo, okay, whatever you have
redirect your standard output into, I don't know, into here.
It doesn't matter which one. So, just whatever you have don't put it in the
standard output, because this is standard output when it prints it out to your
terminal screen. Don't print it out to the standard output, rather instead
please redirect it to my file and put it there. So that's a very nice feature.
These things are rather universal, escape characters, greater than sign. So you are
able to in fact redirect, this is not network traffic, but
you are able to basically steer the traffic within your machine in a way you
want it, in a way you want it to function, towards a place where you want it to go.
And it's fairly simple, there isn't that much complication to it.
Now let me just use this echo command in combination with the grep command, because
that is yet another thing that I wanted to show you. And on top of that, we will
need a pipe. Look, this is a pipe. This is a piping sign binder between two
commands and it is basically saying pipe it through. So whatever you have, whatever
your output, so whatever the echo outputs, and this is the output of the echo.
Please pass it along, this says the pipe, please pass it along to the command that
will stand on this side. And on this side we will use grep, which basically just
pulls something out, grep, and we will say that, I don't know, we want to pull...what
shall we pull? I don't know, did. And if I press Enter, there you go, it
has symbolized it completely red. Let's make a file now.
Actually, no, we already have a lot of files here that we can, I'm sure we can
use at least some of them. What do we got in echo? cat echo, excellent! So we have
Hey, but this is not enough. We should do echo, I don't know, hey there.
Then the newline character, how are you, I feel random.
Amazing, right? Makes no sense of whatsoever, but doesn't really matter.
You need to get lost and go into the echo. It actually
didn't recognize the newline characters. Okay, no problems. Let's go and search the
man pages for that. Enable interpretation of backslash escapes. Oh, I forgot the e,
but it seemed to be able to recognize some of the things. Backspace, produce,
escape, form, new line. Okay, so I am correct in using this, but there is one
thing that I am missing here which is lowercase e. Okay, I need a space
here. There you go. So, cat ECHO, there you go.
So, clear the screen, cat echo. And if you're wondering what these two spaces
are, they were in the original echo. You can see them here. So, if I was to delete
a space here, and delete a space here, press ENTER, and cat ECHO. No,
why do you have those tabs there? Come on my good man, come on.
What is the deal? I got no idea. It doesn't really matter for the time being.
Maybe I'm doing something wrong with the echo command, but that's beside
the point. What I wanted to show you is the file itself. So, you have have this
weird-looking file with those two spaces in the front, and since you have the file
now imagine that this file was like 400 pages long. And you need to find
particular words, or particular patterns, or, I don't know, you need to find
whatever. How do you do that? Well, okay, sure a lot of text editors will have a
find option, and granted the terminal text editors also
have find options. But, if you're doing something in your terminal, and if you
just want to figure out what a certain variable value is, or if you want to
format it in a way you can output the text
standard output. And then you can pass it on
to grep, actually. And with grab you can take particular lines which you can then
in turn pass to yet another program to do something with them. Now, okay, you might
think that this is a far-fetched scenario, but no this is actually quite
common. So, for example, imagine that you have a program which is scanning for
thousands of IP addresses. And that program is gathering information whether
the port 80 is open or not, it's looking for web servers. Anyway,
you get a lot of information there. I mean, if you're using one of the
well-known tools like nmap you're gonna get a lot of information. Aside from the
port, you might get, I don't know, some verbose output for how it was done.
What's the Geo IP location of that particular IP address, like
where in the world is it located, and so on and so forth. And let's say you just
need a collection of IP addresses which are in the following format. So you have
the IP address, you have a confirmation that the port 80 is open, but you don't
want to put it in the table. You're just saying, okay, I want the IP address, and I
want the city in which this IP address is. You would have to use graphical
output. And then you would need to pass it along to something else, like into a
file, or something of a kind, so it would get sorted in such a fashion. Later on as
we progress, grep will be inevitable. You will see a lot of examples as we use it
because we will simply need it. It's one of the most used commands out there.
Anyway, so now that we have this file ECHO, I just want to show you how you can
use grep in order to actually pull lines from it. So here's
what grep does. It matches the pattern, it finds it, and if it finds it
in a certain line it will print that entire line out. So it won't just print a
word out, it will print the entire line and it will change the color of that
particular pattern which was found in that particular line, so that you know
that it's there. Anyway, let's go ahead and say cat ECHO. And there are countless
ways of doing this, I'm just used to doing it like this, but I'm sure that
somebody out there knows a simpler way. grep, by simpler I mean typing less.
However, I found this to be a relatively easy, a simple example to follow through
to build your foundation. Anyway, cat ECHO |. So, pass
whatever you plan to put into standard output, don't, pass it to grep and then
grep will actually pull something out of here. Let's say I want grep to pull, I
don't know, hey. Pull hey, excellent! So you see it has printed out the line hey
there. Grep is case sensitive, and you can type in -i to ignore the case. You
see that's pretty much the same, and look it will still print out hey there.
Let's see, eal, excellent! You see it has matched a pattern and it has printed
that line out. So let's see if I have some common patterns that are the same
for at least two lines.
Okay, so grep e. You see how this will actually print out all three of
them. Let's see with h, excellent! With h it will print out only two of the
lines. So you see you can pass all sorts of patterns here, without any sort of
problems whatsoever, and it will go for it and look for them.
Now, as I said, we will use grep extensively in
this tutorial, and you will see a great deal more of grep. That is why I would
strongly recommend that you read the man pages on grep as extra reading material.
You might think that it's in vain, or silly, or something of a kind, but the creators
of these programs have written these manual pages. And if you ask any Linux
user out there, any Linux system administrator, they will tell you that
they wouldn't be able to function without the man pages,
or the help in general that you get for the terminal from the system. So,
don't shy away from them, use them. And especially for a grep I would advise
that you do read through it. As you see, you have multiple versions of it.
Actually, these are completely different programs
performing the same purpose as they are a bit more advanced. So you have grep,
egrep, fgrep. egrep is generally used today a lot as well, but for simpler
tasks you can use grep. For something extremely complex you can use egrep as
well. Anyway, I bid you farewell here and I hope to see you in the next tutorial.