- Another common element in a script
is the pattern-matching operator.
The pattern-matching operator
is all about cleaning up strings.
So if a user provides information,
but there's too much information
and you only need to have a part of it,
then pattern-matching operator is what you are going to use.
So how does it work?
Well, in a pattern-matching operator,
you perform an operation on a variable.
If the variable is $1,
which is the first positional parameter
or the first argument that was provided
while running the script,
you can put the $1 between curly braces,
and do ${1#}.
That's not really a pattern-matching operator,
but it prints the length of the string.
And that can be useful.
Now if you would use ${1#string},
then you are going to search for "string"
and you'll remove the shortest match of the string
from the front end of $1.
There's also double-hash,
and that will remove the longest match of the string,
also from the front end.
Then we have %string,
which removes the shortest match of the string,
and %%string,
which removes the longest match of the string
from the back end of $1.
Let's have a look at some examples
in patterns.sh, which is in the course Git repository.
Okay, let's have a look at patterns.sh,
which is a simple example script
which is demonstrating what this is all about.
So in this example script,
we are going to provide a string as argument
while running the script,
like /usr/bin/blah,
but you can use anything you want.
This script is really
a script that is looking for filenames,
so it should look like a filename,
that's the only thing that matters.
Now, what are we going to do?
Well, first we set the variable "filename"
and filename is set to $1,
and then we have double-hash */,
So we are looking for */,
which is a / with anything in front of it.
Then we are looking for the longest match of this pattern.
What is the longest match?
Well, if we look from left to right in the string,
now we are really searching for the last slash,
and we remove it with all that is in front of it,
so that the filename is remaining.
And then we use echo filename is '${1 hash-hash */}'
between single quotes.
The single quotes make
that we escape the pattern-matching operator,
and we just print what is going to happen.
That's easy for understanding what is going to happen.
And in the next line, the name of the file is $filename.
That is printing the "filename" variable
that was set according to the pattern-matching operator.
We do something similar for the directory name,
but this time the pattern-matching operator is %/*.
What does that mean?
That means that from right to left
we are searching for the pattern / followed by anything.
It's a single %, so that will be shortest match.
And the shortest match in this example would be /blah.
So the remaining parts is the directory name, /usr/bin,
and that is what's going to be printed.
Let's run it so that we can see what's going to happen.
(keyboard clacking)
So in the script it is important
that we provide an argument, /usr/bin/whatever.
Really, it's not really important
if it's an existing file or anything,
it's just about what the pattern-matching operator
is going to do.
And what do we see?
We see that it keeps the filename "whatever"
and the directory name, which is set to /usr/bin.
Now we are going to do something interesting,
and we apply a couple of modifications.
So for filename I am going to make that
no longer hash-hash, but single-hash */.
And for directory name, I make that double-percent.
So we will be going in the opposite direction here,
and then we are going to check what is going to happen.
I would like to invite you
to try to understand what is going to happen
before running it.
And once you have an idea, then you run it.
To give you a hint, single-hash is shortest match.
And to give you another hint, * is zero or more characters.
So we are looking for the shortest match
of a / with zero or more characters in front of it.
Let's check it out.
And there you can see what the single-hash */ is doing.
It's just removing the slash in the beginning.
So the remaining part is usr/bin/whatever.
And for the directory name,
well, the directory name
is looking for the longest match
of the pattern / followed by anything,
and that happens to be the entire pattern.
So the name of the directory is nothing at all.
And that's how you can use pattern-matching operators.
Pattern-matching operators are clean and they are fast.
They are best internal solution,
so you don't require anything to be loaded,
and that is why you see pattern-matching operators
quite often in scripts.