Command Line Arguments and Positional Parameters

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
>> Hey, there Cybrarians.
00:00
Welcome back to the Linux plus course here at Cybrary,
00:00
I'm your instructor Rob Goelz.
00:00
In today's lesson we're going to be discussing
00:00
command line arguments and positional parameters.
00:00
Upon completion of this lesson,
00:00
you're going to be able to understand
00:00
the purpose of command line arguments and
00:00
positional parameters and explain why you might
00:00
want to accept or use
00:00
command line arguments in a script,
00:00
and then later in our demo,
00:00
we'll use positional parameters and
00:00
command line arguments in some scripts.
00:00
Key feature of scripting and programming is the ability
00:00
to pass data into a script during run-time.
00:00
One way we can do this is to use
00:00
what are called command line arguments.
00:00
That's really just data that gets provided
00:00
from the command line when you run a script.
00:00
It's helpful when you need to ask a user
00:00
their input to run a command or for example,
00:00
maybe you want to personalize
00:00
output with the user's name,
00:00
or maybe you just need information from
00:00
the customer on what to do in the command.
00:00
Command line arguments are provided after
00:00
the command and they're separated by a space.
00:00
For example, command, space,
00:00
first arguments, space,
00:00
second argument, and so on and so forth.
00:00
Now we can retrieve the values
00:00
of command line arguments using
00:00
special variables that are known as
00:00
positional parameters in BASH.
00:00
For example, the first command line argument we talked
00:00
about is stored in the variable $1,
00:00
and the second is stored in $2, and so on.
00:00
Let's take a look at all of this
00:00
in a script with some demo time.
00:00
Here we are back in our demo environment
00:00
and let's go ahead and open
00:00
a new script that I've created.
00:00
This one is called cla_and_PP.sh.
00:00
What this is, is just a script that demonstrates
00:00
using positional parameters and command line arguments.
00:00
For example, what this clip will do is it will
00:00
print out the positional parameter 1,
00:00
positional parameter 2,
00:00
and then will use that information
00:00
to display the home directory of user.
00:00
It's going to take in the user's name,
00:00
the username for the user,
00:00
and then will use that information
00:00
to display the user's home directory.
00:00
We can run this script and have it
00:00
use that information by doing this,
00:00
we can do cla_and_PP.sh and then I can pass in my name,
00:00
and I can pass in my username,
00:00
and these are the two positional parameters,
00:00
so I'm passing in as command line argument 1 is
00:00
my name and command line argument 2 as my username,
00:00
and then if I hit "Enter",
00:00
this will display the information we saw in the script.
00:00
The user's name is Rob Goelz.
00:00
It gets that, the positional parameter from
00:00
the command line argument 1, my username is Rob,
00:00
it gets that from command line argument 2 and
00:00
positional parameter 2 is printed,
00:00
and then it uses that information in positional parameter
00:00
2 to print the home directory for me.
00:00
That's great, but how does user know what to
00:00
provide the script without actually
00:00
reading the script like we just did?
00:00
Well, another way to accept
00:00
command line arguments is using the read command.
00:00
We could go ahead and modify
00:00
our script and prompt
00:00
the user by printing a question to the screen.
00:00
Then we'll use the read command to
00:00
store the user input in a variable.
00:00
Let's take a look at that in
00:00
a second version of the scripts,
00:00
so we'll do cla_and_PPstep2.sh.
00:00
We'll go in this file, and
00:00
here we can see what I'm talking about.
00:00
What we're going to do here is we're going to
00:00
echo a question to
00:00
the screen; what is your full name?
00:00
Then our script is going to take in
00:00
the content from the user at the command line.
00:00
It's going to read in
00:00
a variable we're going to call name,
00:00
and then what is your username?
00:00
It's going to read in username.
00:00
Then it's going to use that information to print
00:00
the message to the screen to
00:00
customize the output like we talked about.
00:00
For example, it's going to say your full name is name,
00:00
it's going to do variable expansion
00:00
on the positional parameter that we gave it here,
00:00
the variable we gave it is name,
00:00
and one is your username,
00:00
and it will display the home directory using username,
00:00
so it expands that variable and use it
00:00
to display that information.
00:00
Let's see how this runs.
00:00
Let's go ahead and escape:WQ and now what we can do is
00:00
we can do cla_and_PPstep2.sh.
00:00
Now we don't need to pass
00:00
the command line arguments
00:00
to it right on the command line,
00:00
it's going to prompt us to give it that information.
00:00
It says, what is your full name?
00:00
Well, I'll put in Rob Goelz and hit "Enter".
00:00
Now it's going to ask me what's my username,
00:00
and I can just type in Rob.
00:00
What it's going to do is display
00:00
the output customized for
00:00
me with the information I provided.
00:00
So it takes my full name that I provided it,
00:00
it prints that out and then it uses my username
00:00
to display the home directory for me.
00:00
With that, we've reached end in this lesson.
00:00
In this lesson we covered the purpose of
00:00
command line arguments and positional parameters,
00:00
we talked about why you might want to accept
00:00
or use command line arguments in a script,
00:00
and then during our demo we got to see how we could use
00:00
command line arguments and
00:00
positional parameters in scripts.
00:00
Thanks so much for being here and I look
00:00
forward to seeing you in the next lesson.
Up Next