1 00:00:00,000 --> 00:00:11,480 In this video, you'll learn about conditional statements that you can use in shell scripts. 2 00:00:11,480 --> 00:00:14,560 These are what makes shell scripting really powerful. 3 00:00:14,560 --> 00:00:17,200 So different conditional statements are available. 4 00:00:17,200 --> 00:00:19,280 There is if-then-else. 5 00:00:19,280 --> 00:00:23,240 So if allows you to check if a certain condition is true. 6 00:00:23,240 --> 00:00:28,740 And the condition can be a file that exists, it can be a command that executes successfully, 7 00:00:28,740 --> 00:00:33,139 or it can be one of the many results of the test command. 8 00:00:33,139 --> 00:00:35,259 Now we have while do done. 9 00:00:35,259 --> 00:00:41,340 While is what you want to use to run commands as long as a specific condition is true. 10 00:00:41,340 --> 00:00:43,820 Until is the opposite of while. 11 00:00:43,820 --> 00:00:50,540 And case allows you to evaluate a series of elements where you are expecting specific 12 00:00:50,540 --> 00:00:51,540 arguments. 13 00:00:51,540 --> 00:00:53,759 You can check each of these arguments. 14 00:00:53,759 --> 00:00:55,820 Also very popular is for. 15 00:00:55,820 --> 00:00:58,419 For allows you to iterate over a range. 16 00:00:58,419 --> 00:01:00,660 Let's work out an example. 17 00:01:00,660 --> 00:01:05,620 To understand the example script, we need to have a look at the test command. 18 00:01:05,620 --> 00:01:09,320 The test command is a very important command in shell scripts. 19 00:01:09,320 --> 00:01:13,419 It allows you to test so many things, picking out a couple of them. 20 00:01:13,419 --> 00:01:18,480 One of them is minus Z string, for instance, which is checking to see if a string value 21 00:01:18,480 --> 00:01:21,800 is zero, which means that it is empty. 22 00:01:21,800 --> 00:01:25,919 Or you have the equals sign where you can compare strings. 23 00:01:25,919 --> 00:01:31,800 This EQ is comparing integers, and integers have the benefit that you can do GE for greater 24 00:01:31,800 --> 00:01:34,360 equal and less equal, and so on. 25 00:01:34,360 --> 00:01:38,919 A little bit lower, we have the different file tests, so you can test if it's a block 26 00:01:38,919 --> 00:01:43,480 device, or a character device, or a directory, or if the file exists. 27 00:01:43,480 --> 00:01:46,599 And so many more properties can be tested. 28 00:01:46,599 --> 00:01:50,260 Now the test command can be written in two ways, and that's what I'm going to show you 29 00:01:50,260 --> 00:01:52,160 in script two. 30 00:01:52,160 --> 00:01:58,360 So in script two, as always, I'm starting with the shebang, binbash. 31 00:01:58,360 --> 00:02:03,279 Then a white line, because it is nice to have white lines so that you can easily distinguish 32 00:02:03,279 --> 00:02:05,419 the different parts of the script. 33 00:02:05,419 --> 00:02:11,639 And then I'm using if square bracket minus Z dollar one, which can also be written in 34 00:02:11,639 --> 00:02:17,000 an alternative way, if test minus Z dollar one. 35 00:02:17,000 --> 00:02:21,360 I'm putting the second way behind the hash sign because I don't want to run both of them, 36 00:02:21,360 --> 00:02:25,639 but I need you to understand that both of them are completely equivalent. 37 00:02:25,639 --> 00:02:30,320 So the test command, alternatively, can be written as an opening square bracket with 38 00:02:30,320 --> 00:02:34,119 a closing square bracket to the end of the line. 39 00:02:34,119 --> 00:02:40,199 Then we have the then statement, and in the then statement, I'm providing echo, you have 40 00:02:40,199 --> 00:02:45,960 to provide an argument. 41 00:02:45,960 --> 00:02:48,279 And then I'm using exit six. 42 00:02:48,279 --> 00:02:50,720 And what is exit six? 43 00:02:51,080 --> 00:02:56,279 Well, if the condition of we don't have an argument is true, then the script needs to 44 00:02:56,279 --> 00:02:57,279 stop. 45 00:02:57,279 --> 00:03:03,479 And if the script is stopping, then the convention is the exit code is used to inform the person 46 00:03:03,479 --> 00:03:06,839 who ran the script that something has gone wrong. 47 00:03:06,839 --> 00:03:10,000 So exit six is generating an exit code six. 48 00:03:10,000 --> 00:03:12,800 The convention is that is not exit code zero. 49 00:03:12,800 --> 00:03:15,679 So that indicates that something has gone wrong. 50 00:03:15,679 --> 00:03:22,440 And otherwise, I'm printing echo, the argument is dollar one. 51 00:03:22,440 --> 00:03:27,440 Dollar one is the first argument that is used while running the script. 52 00:03:27,440 --> 00:03:31,240 Now fun fact, I'm going to expand it a little bit. 53 00:03:31,240 --> 00:03:33,720 You can also have multiple arguments. 54 00:03:33,720 --> 00:03:45,559 So let me use echo, the number of arguments is dollar hash, it's the argument counter. 55 00:03:46,440 --> 00:03:53,039 And then I'm going to use for i in dollar at, dollar at is a collection of all arguments. 56 00:03:53,039 --> 00:04:02,119 I'm going to use do, echo, this is argument dollar i don. 57 00:04:02,119 --> 00:04:03,119 So what do we see here? 58 00:04:03,119 --> 00:04:06,600 We see a small iteration over all the arguments. 59 00:04:06,600 --> 00:04:08,600 The for loop works as follows. 60 00:04:08,600 --> 00:04:12,160 So dollar at contains a collection of arguments. 61 00:04:12,160 --> 00:04:15,639 And for i in is going to iterate over all of them. 62 00:04:15,639 --> 00:04:21,200 And while iterating over one specific value, that value is stored in the variable i. 63 00:04:21,200 --> 00:04:26,399 And that is why echo, this is argument dollar i, will show you the current value. 64 00:04:26,399 --> 00:04:28,000 Let's see if this works. 65 00:04:28,000 --> 00:04:34,399 So I'm going for script two, oh, chmod plus x, of course, on script two. 66 00:04:34,399 --> 00:04:36,559 Then I'm going for script two. 67 00:04:36,559 --> 00:04:38,799 Oh, you have to provide an argument. 68 00:04:38,799 --> 00:04:41,640 That is the if minus z kicking in. 69 00:04:41,640 --> 00:04:45,959 And to verify the exit code, I can use echo, dollar, question mark. 70 00:04:45,959 --> 00:04:50,640 Echo, dollar, question mark will always show you the exit code of the last command. 71 00:04:50,640 --> 00:04:54,959 Now I'm going to use script two, a, b, c, d, e. 72 00:04:54,959 --> 00:04:57,480 And there we can see the argument is a. 73 00:04:57,480 --> 00:04:59,440 That is for dollar one only. 74 00:04:59,440 --> 00:05:01,279 Here we have the argument counter. 75 00:05:01,279 --> 00:05:05,920 And here we can see that the iteration is working all right as well. 76 00:05:05,920 --> 00:05:11,239 And that allows you to work with these conditional statements that make it possible 77 00:05:11,239 --> 00:05:14,000 to do fantastic things in your scripts.