1
00:00:00,000 --> 00:00:11,500
In this video, you learn about conditional statements that you can use in shell scripts.

2
00:00:11,500 --> 00:00:16,020
These are what makes shell scripting really powerful. So different conditional statements

3
00:00:16,020 --> 00:00:22,100
are available. There is if-then-else. So if allows you to check if a certain condition

4
00:00:22,100 --> 00:00:27,260
is true. And the condition can be a file that exists, it can be a command that executes

5
00:00:27,260 --> 00:00:33,680
successfully, or it can be one of the many results of the test command. Now we have while

6
00:00:33,680 --> 00:00:39,360
do done. While is what you want to use to run commands as long as a specific condition

7
00:00:39,360 --> 00:00:48,400
is true. Until is the opposite of while. And case allows you to evaluate a series of elements

8
00:00:48,400 --> 00:00:53,759
where you are expecting specific arguments. You can check each of these arguments. Also

9
00:00:53,759 --> 00:01:00,639
very popular is for. For allows you to iterate over a range. Let's work out an example. To

10
00:01:00,639 --> 00:01:05,959
understand the example script, we need to have a look at the test command. The test

11
00:01:05,959 --> 00:01:12,040
command is a very important command in shell scripts. It allows you to test so many things,

12
00:01:12,040 --> 00:01:16,400
picking out a couple of them. One of them is minus Z string, for instance, which is

13
00:01:16,400 --> 00:01:22,199
checking to see if a string value is zero, which means that it is empty. Or you have

14
00:01:22,199 --> 00:01:29,160
the equals sign where you can compare strings. Minus EQ is comparing integers, and integers

15
00:01:29,160 --> 00:01:34,760
have the benefit that you can do GE for greater equal and less equal, and so on. A little

16
00:01:34,760 --> 00:01:39,400
bit lower, we have the different file tests. Yes, so you can test if it's a block device

17
00:01:39,400 --> 00:01:45,059
or a character device or a directory or if the file exists. And so many more properties

18
00:01:45,059 --> 00:01:49,760
can be tested. Now the test command can be written in two way, and that's what I'm going

19
00:01:49,760 --> 00:01:56,400
to show you in script two. So in script two, as always, I'm starting with the shebang,

20
00:01:56,400 --> 00:02:02,279
binbash. Then a white line, because it is nice to have white lines so that you can easily

21
00:02:02,279 --> 00:02:08,000
distinguish the different parts of the script. And then I'm using if square bracket minus

22
00:02:08,000 --> 00:02:15,880
Z dollar one, which can also be written in an alternative way. If test minus Z dollar

23
00:02:16,039 --> 00:02:20,520
one. I'm putting the second way behind the hash sign because I don't want to run both

24
00:02:20,520 --> 00:02:25,679
of them, but I need you to understand that both of them are completely equivalent. So

25
00:02:25,679 --> 00:02:30,839
the test command alternatively can be written as an opening square bracket with a closing

26
00:02:30,839 --> 00:02:36,000
square bracket to the end of the line. Then we have the then statement, and in the then

27
00:02:36,000 --> 00:02:47,559
statement, I'm providing echo, you have to provide an argument. And then I'm using exit

28
00:02:47,559 --> 00:02:55,520
six. What is exit six? Well, if the condition of we don't have an argument is true, then

29
00:02:55,520 --> 00:03:00,199
the script needs to stop. And if the script is stopping, then the convention is the exit

30
00:03:00,320 --> 00:03:05,600
code is used to inform the person who ran the script that something has gone wrong.

31
00:03:06,679 --> 00:03:12,479
So exit six is generating an exit code six. The convention is that is not exit code zero.

32
00:03:12,600 --> 00:03:18,119
So that indicates that something has gone wrong. And otherwise, I'm printing echo, the

33
00:03:18,119 --> 00:03:26,240
argument is dollar one. Dollar one is the first argument that is used while running the

34
00:03:26,240 --> 00:03:32,479
script. Now fun fact, I'm going to expand it a little bit. You can also have multiple

35
00:03:32,479 --> 00:03:44,080
arguments. So let me use echo, the number of arguments is dollar hash, it's the argument

36
00:03:44,080 --> 00:03:52,119
counter. And then I'm going to use for i in dollar at, dollar at is a collection of all

37
00:03:52,119 --> 00:04:02,800
arguments, I'm going to use do, echo, this is argument, dollar i, don. So what do we see

38
00:04:02,800 --> 00:04:08,839
here, we see a small iteration over all the arguments. The for loop works as follows. So

39
00:04:08,880 --> 00:04:15,000
dollar at contains a collection of arguments. And for i in is going to iterate over all of

40
00:04:15,000 --> 00:04:20,839
them. And while iterating over one specific value, that value is stored in the variable i.

41
00:04:21,119 --> 00:04:26,799
And that is why echo, this is argument dollar i, will show you the current value. Let's see if

42
00:04:26,799 --> 00:04:34,799
this works. So I'm going for script two, oh, chmod plus x, of course, on script two, then I'm

43
00:04:34,799 --> 00:04:41,720
going for script two. Oh, you have to provide an argument. That is the if minus z kicking in. And

44
00:04:41,720 --> 00:04:47,279
to verify the exit code, I can use echo, dollar, question mark. Echo, dollar, question mark will

45
00:04:47,320 --> 00:04:54,480
always show you the exit code of the last command. Now I'm going to use script two, a, b, c, d, e.

46
00:04:54,839 --> 00:05:00,480
And there we can see the argument is a, that is for dollar one only. Here we have the argument

47
00:05:00,480 --> 00:05:07,000
counter. And here we can see that the iteration is working all right, as well. And that allows

48
00:05:07,000 --> 00:05:13,279
you to work with these conditional statements that make it possible to do fantastic things in

49
00:05:13,279 --> 00:05:14,279
your scripts.

