- [Narrator] Comparisons and Tests
In the previous video, we learned
about field separators and iterators.
This video will teach you how to perform tests
that are compatible with Unix system-level features.
Flow control in a program is handled
by comparison and test statements.
Bash also comes with several options to perform tests
that are compatible with Unix system-level features.
We can use if, if else, and logical operators
to perform tests and certain comparison operators
to compare data items.
There is also a command called Test
available to perform tests. Let's see how to use these.
We'll have a look at all the different methods
used for comparison and performing tests,
using an if condition,
using else, if and else.
Nesting is also possible with if and else.
The if conditions can be lengthy
and to make them shorter, we can use logical operators
as execute if the condition is true,
execute if the condition is false,
it's illogical and operation, it's illogical or operation.
This is very helpful trick while writing Bash scripts,
performing mathematical comparisons.
Usually conditions are enclosed in square brackets.
Note that there is a space between
open and close square brackets and operands.
It will show an error if no space is provided.
An example is, performing mathematical conditions
over variables or values can be done as
Other important operators are greater than
less than, greater than or equal to,
less than or equal to.
Multiple test conditions can be combined as
file system related tests.
We can test different file systems related attributes
using different condition flags as
this returns true if a given variable
holds a regular file path or file name.
This returns true if a given variable
holds a file path or file name is executable.
This returns true if a given variable
holds a directory path or directory name.
This returns true if a given variable
holds parts of a character device file.
This returns true if a given variable
holds an existing file.
This returns true if a given variable
holds the path of a blocked device file.
This returns true if a given variable
holds the path of a file that is writable.
This returns true if a given variable
holds the path of a file that is readable.
This returns true if a given variable
holds the path of assuming.
An example of the usages, string comparisons.
While using string comparison, it's best
to use double square brackets, since the use
of single brackets can sometimes lead to errors.
Two strings can be compared to check
whether they are the same, in like this.
It returns true when str1 equals str2
That is the test contents of str1 and str2 are the same.
It's an alternate method for string equality check.
We can check whether two strings are not the same as this.
It returns true when str1 and str2 mismatch.
We can find out the alphabetically smaller
or larger string as it returns true
when str1 is alphabetically greater than str2.
This returns true when str1
is alphabetically lesser than str2.
Note that a space is provided after and before equals.
If it is not provided, it is not a comparison,
but becomes an assignment status.
This returns true if str1 holds an empty string.
This returns true if str1 holds a non-empty string.
It's easier to combine multiple conditions
using logical operators such as and and or.
In this code, for example,
output would be like this.
The test command can be used
for performing condition checks.
It also helps to avoid usage of many braces.
The same set of test conditions enclosed
within brackets can be used for the test command.
For example
Also, in this video, we've seen about comparison and tests.
This video concludes the first section of this course.
We've learned about printing in the terminal,
and we've moved on to playing with variables
and environment variables.
After that, we've seen about function
to propends to environment variables.
Then we moved onto math with a shell.
Also, we've learned to play with
file description and redirection.
Then we've seen about arrays and associative arrays.
We've then learned about visiting aliases.
Also, we've learned how to grab
information about the terminal.
Then we see how to get and set dates or delays.
Then we've seen how to debug the script.
We've then moved on to the topic
of reading the output of a sequel of commands.
Then we learned how to read end characters
without pressing the return key.
We've also seen how to run a command until it succeeds
and then moved onto field separators and iterators.
Finally, we've seen about comparisons and tests.
All of the following videos and all of the next sections
we will see how to have a good command
by using Linux slash Unix.
So the next section will discuss
about how to have a good command.