Conditional Statements

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 and welcome back to
00:00
the Linux plus course here at the Cybrary,
00:00
I'm your instructor Rob Geelz.
00:00
In today's lesson, we're going to be
00:00
talking about conditional statements.
00:00
Upon completion of this lesson,
00:00
you're going to be able to explain
00:00
the purpose of conditional statements,
00:00
as well as differentiate between
00:00
if statements and case statements,
00:00
and then we'll see in our demo at the end of the lesson
00:00
how we can use conditional statements in scripts.
00:00
Conditional statements are sometimes
00:00
known as logic statements,
00:00
and they allow you to test
00:00
>> for specific conditions like,
00:00
>> does a file exist,
00:00
does the user enter any input,
00:00
is the input correct,
00:00
and maybe we can just test
00:00
the result of a mathematical expression.
00:00
Now, based on the result of the test,
00:00
the condition statement can
00:00
branch and perform different actions,
00:00
and we'll see that a little bit later in this lesson.
00:00
Now, there are two types of
00:00
conditional statements that are used in BASH,
00:00
and that is the if statement and the case statement,
00:00
and we're going to look at each statement type
00:00
in depth in the coming slides.
00:00
First of all, if statements.
00:00
If statements use
00:00
the conditional tests that we talked about in
00:00
the last lesson to direct
00:00
script execution based on exit status.
00:00
What does that mean?
00:00
Well, remember when we're doing expressions and
00:00
we're testing things and trying to get the condition,
00:00
we're trying to determine
00:00
whether or not something evaluates to true,
00:00
or zero, or false, or one.
00:00
If we have a tested condition evaluates to true,
00:00
then we want to run commands
00:00
>> immediately under that test.
00:00
>> If it evaluates to false,
00:00
we want to do something else,
00:00
and we can do two different something else's.
00:00
We have two additional logic statements
00:00
we can use in BASH.
00:00
The first one is the l statement,
00:00
and this is pretty cut and dry,
00:00
if this is true,
00:00
do this, if that is true,
00:00
do something else, so if else.
00:00
But we also have the concept of the elif statement,
00:00
which allows you to test
00:00
another condition, so we can say,
00:00
if this is true, do this,
00:00
if that is true,
00:00
do this, else, do a third thing.
00:00
Then in all cases,
00:00
all if statements end with fi,
00:00
which is just if backwards,
00:00
but that's how you close an if statement.
00:00
We can see an example of this on
00:00
the bottom right-hand corner of the image here,
00:00
and what this is doing,
00:00
remember we have those square brackets,
00:00
so what we're doing is we're doing a
00:00
numerical condition test,
00:00
and what is actually happening here is it's
00:00
expanding out this variable user integer,
00:00
and it's dividing that by two.
00:00
Now, it's actually doing modular division,
00:00
that's what that percent sign is there,
00:00
and what that means is it's just trying to
00:00
determine if there's any remainder.
00:00
If we divide a number by two and we
00:00
have no remainder, what does that tell us?
00:00
It tells us that the number is even.
00:00
So user integer divided by two with no remainder,
00:00
or equal zero,
00:00
then we can echo out the number is even.
00:00
But if there is a remainder,
00:00
then we know that the number is odd.
00:00
If the number divided by two has no remainder,
00:00
then echo the number is even else, because it's false,
00:00
echo the number is odd,
00:00
and then we finalize or close that with fi.
00:00
Now, a case statement is
00:00
just a more elegant conditional statement,
00:00
but it is really useful when you have to test
00:00
multiple conditions and perform
00:00
a different action or command for each,
00:00
it's just a little bit easier to read as well.
00:00
Let's take an example of testing the day of the week.
00:00
We could write a test statement to do this.
00:00
We can say case day in,
00:00
and then what will happen is,
00:00
it'll expand that variable day
00:00
and determine what day of the week it is,
00:00
and based on what day of the week
00:00
it is, it will display output.
00:00
For example, if the key statement expands day,
00:00
and it says that the day is Monday,
00:00
that's going to echo that, I suggest
00:00
a lot of coffee and so on and so forth.
00:00
It's going to go down and test all of
00:00
these conditions and determine what the day is.
00:00
For example, if you get to Thursday,
00:00
it'll say one day till Friday,
00:00
or if Saturday or Sunday,
00:00
it'll say echo is the weekend, enjoy yourself,
00:00
and then just like we saw with the if statement,
00:00
we close that case statement by spelling it backwards,
00:00
so case spelled backwards is esac,
00:00
and that's how we close a case statement.
00:00
Let's take a look at some
00:00
>> conditional tests we can do in
00:00
>> a script with some demo time.
00:00
Here we are in our demo environment,
00:00
and today we're back here in Ubuntu,
00:00
and what we're going to do is we're going to
00:00
take a look at a new script.
00:00
Now, I've already started
00:00
creating the script a little bit,
00:00
so let's open it up and take a look,
00:00
this script will be called condition state data Sage,
00:00
and you might be saying, wait,
00:00
this script looks familiar, and it is,
00:00
because what we're going to do here
00:00
>> is we're going to use
00:00
>> our existing script and do some work inside of here.
00:00
What we're doing in this script is
00:00
the same thing we were doing before when we
00:00
were taking command line arguments
00:00
and positional parameters,
00:00
but what we're going to do now is we're actually going
00:00
to add some tests here to
00:00
determine whether or not
00:00
>> we've actually gotten value from
00:00
>> the user before we go forward and print anything.
00:00
For example, what we might want to do here
00:00
first and foremost is test and make
00:00
sure that we've gotten content from
00:00
the customer or the user,
00:00
and get that information
00:00
into that name variable that we read in.
00:00
We could do that pretty easily. We could do if,
00:00
and we're going to say -Z $name,
00:00
and then what we'll do is if
00:00
this does not have any content,
00:00
because remember the -Z
00:00
indicates that a string has a length of zero,
00:00
so that means that nothing
00:00
was passed into this variable,
00:00
it's zero length, it has nothing in it,
00:00
then what we'll do is we'll say,
00:00
then echo, and we'll display a message to
00:00
the screen that'll say, no name provided.
00:00
We can do another conditional test.
00:00
We can say elif,
00:00
and we could say same thing.
00:00
Let's test and make sure that the username is provided,
00:00
so we could do dash Z username,
00:00
and this will indicate whether or not we
00:00
have any length to this username, if there's no length,
00:00
then we want to do something else,
00:00
and in this case, what we'll do
00:00
is we'll do another echo,
00:00
and we'll say that no username provided,
00:00
please enter your username,
00:00
and then finally we can close out
00:00
our statement with a final else,
00:00
so we just hit else here,
00:00
and then what we could do is we
00:00
could echo something else,
00:00
because basically we're going to say, hey,
00:00
if this does work,
00:00
if these two tests passed,
00:00
then we can run the same commands we
00:00
did when we did this in our previous script.
00:00
So we can say your full name is dollar sign name,
00:00
and we could say that
00:00
your home directory contains
00:00
the following content,
00:00
and then what we can do is actually use the username
00:00
provided to display that content.
00:00
Then finally, the last thing that we
00:00
always have to do with any if statement
00:00
is use fi to close out the statement.
00:00
Let's go ahead and hit "Escape:wq" and see how we did.
00:00
Now, if we run this command,
00:00
we run condition state data Sage,
00:00
is going to prompt us for the full name,
00:00
is going to prompt us for the username,
00:00
we're going to provide neither,
00:00
and it's going to say no name provided
00:00
because that's the first condition we test.
00:00
Let's run this again,
00:00
and this time we'll provide my name,
00:00
but I'll provide no username, and I'll hit "Enter",
00:00
and it'll say, no username provided,
00:00
please enter your username.
00:00
Let's do both now,
00:00
say my full name is Rob Geelz,
00:00
my user name is Rob,
00:00
and we can see that it evaluates to
00:00
the last condition and
00:00
runs the script like we did previously,
00:00
it prints the user name and it prints
00:00
the user home directory and the content in it.
00:00
That's really all we need to
00:00
know about condition statements,
00:00
so we can see that condition statements here and
00:00
the format a little bit of using an if statement.
00:00
With that, we've reached the end of this lesson.
00:00
In this lesson we covered
00:00
the purpose of conditional statements,
00:00
we differentiated between if statements and case
00:00
statements and determined when
00:00
each one of those might be used,
00:00
and then we used conditional statements
00:00
in 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