File Globbing and Shell Expansions

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or

Already have an account? Sign In »

Time
21 hours 25 minutes
Difficulty
Intermediate
CEU/CPE
21
Video Transcription
00:00
>> Hello, Cybrarians.
00:00
>> Welcome back to
00:00
>> the Linux plus course here at Cybrary.
00:00
I'm your instructor Rob Goelz.
00:00
In today's lesson we are going to be discussing
00:00
File Globbing and Shell Expansions.
00:00
Upon completion of today's lesson,
00:00
you're going to be able to understand
00:00
file globbing and shell expansions in Bash,
00:00
we're going to talk about the purpose of
00:00
file globbing and shell expansion metacharacters.
00:00
Then we'll get to see how to use file globbing and
00:00
shell expansion in some scripts
00:00
doing a demo at the end of this lesson.
00:00
A glob is also known as a wildcard operator,
00:00
and that's just a really a metacharacter that's used to
00:00
match some number of files based on a pattern.
00:00
The shell is responsible for
00:00
expanding that glob and
00:00
listing the matches that it finds,
00:00
using a shell to find matches with
00:00
these glob metacharacters is known as file globbing.
00:00
There are four glob types to know, four metacharacters.
00:00
There's the asterisk which
00:00
matches zero or more characters.
00:00
There's the question mark which
00:00
matches any single character,
00:00
and then there's the left and right square brackets.
00:00
What happens with those is that
00:00
any character within the two brackets is matched.
00:00
File globbing is really just an example
00:00
of shell expansion,
00:00
but there are other expansion features to know as well.
00:00
In our demo earlier we saw that we can print
00:00
variables stored with the dollar sign metacharacter.
00:00
If you do echo dollar sign variable name,
00:00
that's known as variable expansion.
00:00
We can print that information in
00:00
the variable by using that metacharacter.
00:00
But what happens if you try to
00:00
display a variable that hasn't been said?
00:00
Well, by default, we saw this previously
00:00
when we were playing around with those local variables.
00:00
If the bash shell doesn't know about it,
00:00
it just returns a null string. An empty line.
00:00
That can be confusing, if nothing is returned,
00:00
what does that mean?
00:00
Well, what you can do is you can actually go in and use
00:00
variable expansion metacharacters to return
00:00
a default value to you to tell you that nothing is set.
00:00
If you're working around into script and you want to make
00:00
sure that there's something there or
00:00
display a message if there isn't,
00:00
you can use these metacharacters,
00:00
the dollar sign in the curly braces to
00:00
return something to you to at
00:00
least tell you, hey, nothing's here.
00:00
What you could do is you could do echo,
00:00
dollar sign, left curly brace and then the variable name,
00:00
and then put a colon dash after it,
00:00
and then put in a line that you want to
00:00
print to the screen if there's nothing there.
00:00
For example here we can say print if nothing
00:00
in variable and then close it with a right curly brace.
00:00
Then if we run this and the variable name is empty,
00:00
it will just return print if nothing
00:00
in a variable to our standard output.
00:00
Now another shell expansion metacharacter
00:00
is command substitution,
00:00
and it uses the metacharacters dollar sign
00:00
and then left and right parentheses.
00:00
I have always thought of this as a sub-shell.
00:00
Because it's this shell that's
00:00
executed from within a shell session.
00:00
It's a sub-shell and it
00:00
returns output to the original shell.
00:00
Now this is most commonly used in Bash
00:00
scripting to capture command output in a variable.
00:00
For example, we can capture the output of
00:00
the command that's encapsulated
00:00
inside of the dollar less than writer.
00:00
Then parentheses that command output back
00:00
into a variable doing variable equals dollar sign,
00:00
left parenthesis command right parenthesis.
00:00
Another way we can accomplish this and do
00:00
command substitution is with
00:00
the use of back tick metacharacters.
00:00
We can do variable equals back
00:00
tick Command and then back tick,
00:00
but note that back tick character
00:00
there is not the same as a single quote character.
00:00
For my purposes, I generally prefer to use
00:00
the other command substitution
00:00
using the dollar sign and then the parentheses,
00:00
because it's really easy and your
00:00
script and mistake that back take
00:00
for a single quote and have
00:00
syntax problems when you're trying to hunt that down.
00:00
Let's take a look at all of this with some demo time.
00:00
Here we are back in our demo environment
00:00
and let's go ahead and reopen a script file.
00:00
Now I've started the script file
00:00
just to save us some time.
00:00
I call this one glob and expand.sh.
00:00
What I've done is I've already gone in here and created
00:00
the line with the shebang and the interpreter.
00:00
Obviously we're using Bash,
00:00
and I just added a couple of comment lines
00:00
that is explain what are we doing?
00:00
Let's go ahead and look at this.
00:00
Now what we're going to do here is we're
00:00
going to first start with file globbing.
00:00
Let's go ahead and say that our use case is
00:00
displaying all of our COM files
00:00
that are in the etc directory.
00:00
What we might want to do first and foremost is echo,
00:00
a line that indicates what we're doing.
00:00
We'll say echo and what we'll say here is
00:00
we're printing a list of
00:00
all configuration files in etc elements,
00:00
config your patient, there we go.
00:00
We're going to go ahead and do that.
00:00
Now what we can do in order to do this
00:00
is we can use file globbing to list
00:00
any files that have any number of
00:00
characters before the.com suffix.
00:00
We can do ls and then etc and then star,
00:00
because remember star is the
00:00
file globbing metacharacter that
00:00
indicates any characters before,
00:00
then we can say.com.
00:00
Now we can do is we can hit
00:00
"Escape" colon WQ to save and quit out of that file.
00:00
Now what we'll do is we'll actually make
00:00
this file executable by
00:00
using command u plus x glob and expand.sh.
00:00
Now we can run that. We could run glob and expand.sh.
00:00
We can see that what it does is it
00:00
prints the echo message that we
00:00
had cranial list of all configuration files and etc,
00:00
and then we can see that it actually does
00:00
print all of those files to our terminal.
00:00
Let's go back into our script again
00:00
and we're going to look at shell expansion next.
00:00
We'll go back in here and add a new line.
00:00
What we'll do here is play around variable expansion.
00:00
Let's use a couple of different use cases.
00:00
First of all, let's try and echo
00:00
a variable that doesn't exist.
00:00
We'll do echo on dollar sign.
00:00
Curly brace, no bar.
00:00
What I'm going to do is I'm actually going to
00:00
escape and hit "YY",
00:00
because I want to copy this and I want to use it again.
00:00
Now what I'm going to do is I'm going to
00:00
give us another use case,
00:00
which is maybe we'll print
00:00
something if this variable doesn't exist.
00:00
The way that we could do that is by doing colon dash.
00:00
Then we could give it a message to print.
00:00
We'll just say print if no variable found.
00:00
Then what I can do is I can go
00:00
actually and assign a variable to this as well.
00:00
I'll go down here and created a new line.
00:00
Then what I'll do is I'll assign a variable to know VAR.
00:00
I'll say that this is,
00:00
there is a variable here. I'll close that.
00:00
Now what I can do, I'm going to hit "Escape" to get back.
00:00
I'm going to hit "YY" and I'm going to print this again.
00:00
I'm hitting "P" to paste this information back in.
00:00
Now this should either print the variable
00:00
or print this message if no variables found.
00:00
We have three options here.
00:00
If there's no variable found, this will return null.
00:00
If there's no variable found, it should return this,
00:00
then the variable that gets assigned,
00:00
and so if there's a variable assign,
00:00
it should return the variable.
00:00
Otherwise it should return this message.
00:00
Let's see if we did this right,
00:00
I'm going to hit "Escape" colon and WQ.
00:00
Now if I run glob and expand again,
00:00
we see all of the information that we had previously,
00:00
but we also see this new blank line here.
00:00
That's because when we printed
00:00
no variable and we didn't give it a message to print,
00:00
we just printed a no variable.
00:00
That variable didn't exist.
00:00
It returns a null line.
00:00
Just an empty string, empty line.
00:00
Then we have the second option where we said,
00:00
hey, if there isn't a variable found,
00:00
print the no variable message,
00:00
found message, and then we did
00:00
assign a variable to that variable.
00:00
We see that that variable is finally printed.
00:00
We can see all three of those options here as well.
00:00
This is going to return a null screen string.
00:00
This variable hasn't been assigned yet,
00:00
so it displays this message.
00:00
If the variable has been assigned here,
00:00
it displays the variable,
00:00
which is what we see it doing.
00:00
Now let's go ahead and do something else.
00:00
I'm just going to hit
00:00
"Escape" and then I'm going to hit "O",
00:00
start a new line and I'll hit "Enter" to give me
00:00
another new line message or another new line to work on.
00:00
Now let's look at commands substitution.
00:00
What we could do is we could capture
00:00
a command in a variable.
00:00
Remember, we could do this two ways.
00:00
We could do, let's capture a current up time.
00:00
I'll just call it curUptime,
00:00
and what I'll do is I'll assign that using
00:00
the dollar sign parentheses.
00:00
I'm going to capture the uptime command.
00:00
Then we'll also capture another variable.
00:00
In another variable will capture
00:00
another thing called today.
00:00
What we'll do is we'll capture the command output date,
00:00
but here we're going to use the back ticks instead.
00:00
We'll do plus percent d,
00:00
that's going to capture that date.
00:00
I'm going to hit back tick again.
00:00
Notice this light pink syntax highlighting here,
00:00
it shows that we are capturing
00:00
a command and we're capturing into a variable.
00:00
Now let's create two echo lines down here.
00:00
What I'll do is I'll will use these echo lines to
00:00
print the contents of these two variables to the screen.
00:00
What I can say is the current up time is,
00:00
and I'll say dollar of time, because remember,
00:00
we can use the dollar metacharacter
00:00
to expand the variable.
00:00
We'll say today's date is,
00:00
and we'll say dollar
00:00
today because that's our other variable.
00:00
We have our current uptime.
00:00
Sorry, I need to put in curUptime.
00:00
Make sure your variables are right.
00:00
You got to expand the right variables.
00:00
Then we have today here.
00:00
Hopefully, I've done everything right.
00:00
We can go ahead and hit "Escape" colon WQ,
00:00
and we can run glob and expand again.
00:00
We see that those are indeed working properly.
00:00
The current uptime is,
00:00
and then we have the expansion
00:00
of the curUptime variable,
00:00
displays the uptime command
00:00
information command substitution.
00:00
Today's date is captured here as well,
00:00
and we just get the date value
00:00
that came from the date command.
00:00
Again, to look back at that, we see those here.
00:00
We've set these as command substitution,
00:00
assign them to variables.
00:00
We did this with the dollar sign and parentheses,
00:00
as well as the backticks.
00:00
Like I said, just be aware of
00:00
that when you're using backticks,
00:00
this character looks an awful lot like that character.
00:00
If you're not looking real carefully for it,
00:00
it's really easy to miss.
00:00
I'm going to go ahead and hit
00:00
"Escape" colon WQ to back out of this,
00:00
and then we're good to go.
00:00
With that, in this lesson, we covered the concepts
00:00
of file globbing and shell expansion and bash.
00:00
Then we talked about the purpose of
00:00
fog lobbying and shell expansion
00:00
metacharacters like the asterisk,
00:00
the question mark, the left and right square braces,
00:00
dollar sign and curly braces,
00:00
dollar sign and parentheses and the backticks.
00:00
Then we got to see how we can use file globbing and
00:00
shell expansion in Bash scripts during our demo.
00:00
Thanks so much for being here and I look
00:00
forward to seeing you in the next lesson.
Up Next