# Condition Tests

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

Time
21 hours 25 minutes
Difficulty
Intermediate
CEU/CPE
21
Video Transcription
00:00
>> Hello Cybrary. 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're going to be
00:00
talking about condition tests.
00:00
Upon completion of this lesson,
00:00
you're going to be able to understand
00:00
how exit codes, operators,
00:00
and expressions work to test conditions
00:00
>> and then we're going to differentiate
00:00
>> between the different operator types
00:00
that we can use when doing condition testing.
00:00
BASH uses operators to
00:00
evaluate a condition and what this is,
00:00
is an expression in BASH uses these operators
00:00
to determine if that condition is true or false.
00:00
True or false, zero or one.
00:00
We saw this when we were talking about
00:00
exit codes in the previous lesson.
00:00
Because exit codes 0 or success is true
00:00
>> and exit code 1 or failure is false.
00:00
>> Now there are a number of
00:00
different operator types that can be
00:00
used in an expression to
00:00
test if something is true or false.
00:00
They are the string relationship,
00:00
arithmetic, Boolean, and file operators.
00:00
We're going to go into some more detail about each of
00:00
these different operator types in this lesson
00:00
before we move forward with doing
00:00
conditional logic in our scripts.
00:00
String operators are used in an expression
00:00
to compare strings and there are five of these.
00:00
Really what we're doing is we're trying to
00:00
determine what the string length is,
00:00
so we're really comparing the length of a string.
00:00
For example, we can compare
00:00
two strings and determine if they are of equal length.
00:00
We can say string1 equal size string2,
00:00
and that's right as is string1 equal to string2.
00:00
We could also use the greater than
00:00
>> and less than operators that
00:00
>> we've seen previously when we
00:00
were talking about redirection.
00:00
We can use those to compare string length,
00:00
so string1 greater than string2,
00:00
string1 less than string2.
00:00
Then we also have two operators
00:00
to determine the length of a single string.
00:00
In this expression, we're not comparing things,
00:00
we're just wondering,
00:00
you're trying to find out what the length is of
00:00
a single string so -n string1 is,
00:00
is that string greater than 0,
00:00
>> or -z string1 is,
00:00
>> is string1 length of 0.
00:00
What we're trying to figure out is, is it empty,
00:00
it has a string of 0,
00:00
or is it greater than 0?
00:00
In other words, does the string have any content in it?
00:00
Relationship operators are used when we
00:00
want to compare integers or numbers.
00:00
Really, this is just called a numeric condition test.
00:00
Now there are six relationship operators,
00:00
and these don't use the less than,
00:00
greater than signs that we just saw,
00:00
those are for strings.
00:00
When you're talking about comparing numbers,
00:00
there are different operators for doing this.
00:00
Also notice the format,
00:00
when we're doing comparison against numbers,
00:00
we put the comparison or
00:00
the expression inside of the square brackets.
00:00
For example, 1 -eq2 means does 1 equal 2,
00:00
so -eq is
00:00
the equal relationship operator
00:00
and, of course, 1 doesn't equal 2,
00:00
so that returns false or one.
00:00
Now the same thing is true of not equal -ne,
00:00
greater than -gt,
00:00
less than -lt,
00:00
>> and then we finally get down to
00:00
>> greater than or equal to, which is -ge.
00:00
We could use, for instance, open square bracket,
00:00
3 greater than 2,
00:00
close square bracket, and that will return true,
00:00
because 3 is obviously greater than or equal to 2,
00:00
>> and then same thing for less than or equal to,
00:00
>> 1 less than or equal to 2
00:00
>> returns true because 1 is less than or equal to 2.
00:00
>> Now, I said previously that
00:00
the format is different because we can still
00:00
use string operators like we can use
00:00
the less than signs and equal than signs
00:00
that we saw in the previous slide
00:00
but then we have to go in and
00:00
use these double parentheses on
00:00
either side of the expression.
00:00
It's really up to you as to whether or
00:00
not you want to try and memorize
00:00
the relationship operators or
00:00
just use the string operators everywhere.
00:00
But you're going to run into trouble in
00:00
one place or another because you're going to have
00:00
to remember the format is
00:00
different when you're doing comparisons using
00:00
string operators with integers
00:00
versus just comparing strings,
00:00
it's just a little bit different.
00:00
Now arithmetic operations are used to
00:00
perform math, arithmetic operations.
00:00
For example, here we have add and we could do
00:00
example sum equals dollar sign,
00:00
>> square bracket, 1 plus 2, square bracket,
00:00
>> and what that is going to do is it's
00:00
>> actually going to perform the arithmetic inside of
00:00
the square brackets,
00:00
>> and then using that shell expansion,
00:00
>> it assigns the result to the sum,
00:00
>> and so if we were to echo sum,
00:00
>> we'll see that it returns 3.
00:00
>> We can do this for all of
00:00
the other common arithmetic math operations,
00:00
2 minus 1,
00:00
>> we can multiply, we do that with the asterisk character
00:00
>> 2 times 2,
00:00
>> and we can divide,
00:00
>> we do the forward slash character for division,
00:00
>> 2 divided by 2,
00:00
>> and then we can do the same trick.
00:00
>> Note here when we talked about previously,
00:00
when we did numeric comparisons,
00:00
we use the square brackets.
00:00
We also use square brackets for arithmetic operators.
00:00
It might be worthwhile just to
00:00
remember that format anyway,
00:00
because you're probably going to need
00:00
to use it if you need
00:00
to perform any kind of arithmetic operations.
00:00
Now, Boolean operators are
00:00
used to join expressions together
00:00
because they can test the truth
00:00
of two or more conditions.
00:00
Really what we could do, for example,
00:00
is we can say, are two things true?
00:00
Which we can say, is this true and is that also true.
00:00
When we use the AND operator,
00:00
that means that both conditions we are testing must be
00:00
true in order to make the overall condition test true.
00:00
For example, 1 equals 1,
00:00
-a or AND,
00:00
2 equals 2,
00:00
and then echo 0, that's going to return 0 or true.
00:00
We can also use the OR operator.
00:00
With the OR operator or -o,
00:00
that's going to indicate that
00:00
either of the expressions must be true.
00:00
For example, 1 equal to 2, well no, that's false.
00:00
Or 2 equal 2,
00:00
well, yeah, that's true.
00:00
Overall, that condition test is true
00:00
because one of them is true and
00:00
as long as either of them are true,
00:00
the entire thing is true.
00:00
Then we have the Boolean NOT operator
00:00
and that just inverse the expression.
00:00
Is 1 equal to 2?
00:00
No, it isn't equal to 2,
00:00
but we inverted that expression so it is true.
00:00
That just inverts the entire expression,
00:00
so returns zero or true.
00:00
Now file operators are used to
00:00
test various file properties.
00:00
There are 16 distinct file operators,
00:00
but we're only going to cover
00:00
some of these that you're going
00:00
to commonly use in using your scripts.
00:00
For example, we could say -e and then the file name
00:00
>> or path to file, and that will
00:00
>> determine whether or not a file exists.
00:00
If you want to check and see if the file size
00:00
is greater than zero,
00:00
we could do -s and then the file name.
00:00
We can check to see if the file is actually a file
00:00
or a text file with -f and then the file name.
00:00
Then we can check to see if the file is a directory,
00:00
in other words, not just a regular file with
00:00
-d for directory and then the file name.
00:00
Then we can see if the file
00:00
has execute permissions set on it for
00:00
the user by using the -x file name operator.
00:00
With that, in this lesson,
00:00
we covered how exit codes,
00:00
operators, and expressions work to test conditions.
00:00
Then we talked about the different operator types:
00:00
string, relationship,
00:00
arithmetic, Boolean, and file operators.
00:00
Thanks so much for being here,
00:00
>> and I look forward to seeing you in the next lesson.
Up Next