In this video, you learn about conditional statements that you can use in shell scripts.
These are what makes shell scripting really powerful. So different conditional statements
are available. There is if-then-else. So if allows you to check if a certain condition
is true. And the condition can be a file that exists, it can be a command that executes
successfully, or it can be one of the many results of the test command. Now we have while
do done. While is what you want to use to run commands as long as a specific condition
is true. Until is the opposite of while. And case allows you to evaluate a series of elements
where you are expecting specific arguments. You can check each of these arguments. Also
very popular is for. For allows you to iterate over a range. Let's work out an example. To
understand the example script, we need to have a look at the test command. The test
command is a very important command in shell scripts. It allows you to test so many things,
picking out a couple of them. One of them is minus Z string, for instance, which is
checking to see if a string value is zero, which means that it is empty. Or you have
the equals sign where you can compare strings. Minus EQ is comparing integers, and integers
have the benefit that you can do GE for greater equal and less equal, and so on. A little
bit lower, we have the different file tests. Yes, so you can test if it's a block device
or a character device or a directory or if the file exists. And so many more properties
can be tested. Now the test command can be written in two way, and that's what I'm going
to show you in script two. So in script two, as always, I'm starting with the shebang,
binbash. Then a white line, because it is nice to have white lines so that you can easily
distinguish the different parts of the script. And then I'm using if square bracket minus
Z dollar one, which can also be written in an alternative way. If test minus Z dollar
one. I'm putting the second way behind the hash sign because I don't want to run both
of them, but I need you to understand that both of them are completely equivalent. So
the test command alternatively can be written as an opening square bracket with a closing
square bracket to the end of the line. Then we have the then statement, and in the then
statement, I'm providing echo, you have to provide an argument. And then I'm using exit
six. What is exit six? Well, if the condition of we don't have an argument is true, then
the script needs to stop. And if the script is stopping, then the convention is the exit
code is used to inform the person who ran the script that something has gone wrong.
So exit six is generating an exit code six. The convention is that is not exit code zero.
So that indicates that something has gone wrong. And otherwise, I'm printing echo, the
argument is dollar one. Dollar one is the first argument that is used while running the
script. Now fun fact, I'm going to expand it a little bit. You can also have multiple
arguments. So let me use echo, the number of arguments is dollar hash, it's the argument
counter. And then I'm going to use for i in dollar at, dollar at is a collection of all
arguments, I'm going to use do, echo, this is argument, dollar i, don. So what do we see
here, we see a small iteration over all the arguments. The for loop works as follows. So
dollar at contains a collection of arguments. And for i in is going to iterate over all of
them. And while iterating over one specific value, that value is stored in the variable i.
And that is why echo, this is argument dollar i, will show you the current value. Let's see if
this works. So I'm going for script two, oh, chmod plus x, of course, on script two, then I'm
going for script two. Oh, you have to provide an argument. That is the if minus z kicking in. And
to verify the exit code, I can use echo, dollar, question mark. Echo, dollar, question mark will
always show you the exit code of the last command. Now I'm going to use script two, a, b, c, d, e.
And there we can see the argument is a, that is for dollar one only. Here we have the argument
counter. And here we can see that the iteration is working all right, as well. And that allows
you to work with these conditional statements that make it possible to do fantastic things in
your scripts.