1 00:00:06,720 --> 00:00:08,370 - The next important part of scripts 2 00:00:08,370 --> 00:00:11,270 That we need to investigate is the conditional statements. 3 00:00:12,240 --> 00:00:15,367 There are different conditional statements that can be used. 4 00:00:15,367 --> 00:00:18,180 "If", "then", "fi" is the first one. 5 00:00:18,180 --> 00:00:20,430 That allows you to execute certain commands 6 00:00:20,430 --> 00:00:22,713 Only if a specific condition is true. 7 00:00:23,647 --> 00:00:28,647 "While" allows you to, to go over a range of something. 8 00:00:29,280 --> 00:00:32,490 Basically as long as a condition is true. 9 00:00:32,490 --> 00:00:34,087 Then you are going to use "while". 10 00:00:34,087 --> 00:00:35,643 "Until" is doing the opposite. 11 00:00:36,570 --> 00:00:39,060 Then we have "case", "case" is where you can use to check 12 00:00:39,060 --> 00:00:41,040 On specific conditions. 13 00:00:41,040 --> 00:00:42,990 And "for" is what you can use 14 00:00:42,990 --> 00:00:45,600 To iterate over a range of items, 15 00:00:45,600 --> 00:00:48,060 To change files for instance. 16 00:00:48,060 --> 00:00:50,730 You will see all of these quite commonly in scripts. 17 00:00:50,730 --> 00:00:53,370 Let me show you a couple of examples. 18 00:00:53,370 --> 00:00:57,030 First, there's a simple example script, "script two" 19 00:00:57,030 --> 00:01:00,293 Which is using "if", "then", "else". 20 00:01:00,293 --> 00:01:02,793 Let me type it and let's analyze. 21 00:01:06,060 --> 00:01:08,913 We need to start with the shebang. 22 00:01:10,830 --> 00:01:15,830 And next, "if", "minus", "Z", "dollar one" 23 00:01:15,930 --> 00:01:19,860 To check if "dollar one" is empty, what is that? 24 00:01:19,860 --> 00:01:21,360 Well, we have the square brackets. 25 00:01:21,360 --> 00:01:24,510 Square brackets are really the test command, 26 00:01:24,510 --> 00:01:27,390 And in this test command we are going to check 27 00:01:27,390 --> 00:01:29,433 And see if "dollar one" is empty. 28 00:01:30,690 --> 00:01:33,180 This can be written in a different way as well. 29 00:01:33,180 --> 00:01:34,860 Just to make sure that you get it, 30 00:01:34,860 --> 00:01:36,750 I've put that in the script. 31 00:01:36,750 --> 00:01:40,740 That will be "if", "dash", "minus", "Z", "dollar one". 32 00:01:40,740 --> 00:01:43,290 Now the amazing thing about the test command, 33 00:01:43,290 --> 00:01:45,480 Let me show you, main test, 34 00:01:45,480 --> 00:01:48,840 Is that you can test on so many items. 35 00:01:48,840 --> 00:01:52,260 So for instance, you can use "minus Z string" 36 00:01:52,260 --> 00:01:53,970 Which is what we are using here to test 37 00:01:53,970 --> 00:01:57,180 If a string is zero, which is the empty string 38 00:01:57,180 --> 00:01:59,580 Or "string one" or "string two", to test 39 00:01:59,580 --> 00:02:04,580 If strings are equal, or "integer one minus eq integer two" 40 00:02:05,190 --> 00:02:07,290 To test if integers are equal. 41 00:02:07,290 --> 00:02:09,930 And I'm just picking out some examples. 42 00:02:09,930 --> 00:02:12,780 Further down you can see tests that you can do 43 00:02:12,780 --> 00:02:14,580 On file types. 44 00:02:14,580 --> 00:02:16,440 So "minus F", if you want to check 45 00:02:16,440 --> 00:02:20,610 If it's a file, or more specifically "minus G" for instance 46 00:02:20,610 --> 00:02:24,000 To check if a file is said group ID. 47 00:02:24,000 --> 00:02:25,530 So there's a lot of things that can be done 48 00:02:25,530 --> 00:02:27,540 With test command. 49 00:02:27,540 --> 00:02:31,470 We are just going to use the "minus Z" option here. 50 00:02:31,470 --> 00:02:35,580 Now what are we going to do if "dollar one" is "minus Z"? 51 00:02:35,580 --> 00:02:38,917 Well then we are going to provide a message. 52 00:02:38,917 --> 00:02:43,917 "Echo you have to provide an argument". 53 00:02:44,340 --> 00:02:45,997 This is also a nice case to use 54 00:02:45,997 --> 00:02:49,320 "Exit", "exit six" for instance. 55 00:02:49,320 --> 00:02:53,370 And next I'm using "fi" to, to quit DF statement. 56 00:02:53,370 --> 00:02:56,760 So only if "dollar one" is empty, then we are 57 00:02:56,760 --> 00:02:58,020 Going to print this message 58 00:02:58,020 --> 00:03:00,360 And we exit with exit code "six". 59 00:03:00,360 --> 00:03:03,240 And in all other cases we are going to use "echo". 60 00:03:03,240 --> 00:03:06,333 The argument is "dollar one" 61 00:03:09,990 --> 00:03:13,383 So let's make this executable and run it. 62 00:03:18,030 --> 00:03:20,160 And there you go, you have to provide an argument. 63 00:03:20,160 --> 00:03:22,680 This is the case where we don't provide an argument. 64 00:03:22,680 --> 00:03:23,910 Now the interesting thing is that 65 00:03:23,910 --> 00:03:27,090 If at this point I'm using "echo dollar question mark" 66 00:03:27,090 --> 00:03:30,840 Then we can see the exit code of the last command 67 00:03:30,840 --> 00:03:32,460 And that's the exit code "six" 68 00:03:32,460 --> 00:03:35,100 The exit code that we have used in the script. 69 00:03:35,100 --> 00:03:38,280 Now this will be particularly useful in bigger scripts 70 00:03:38,280 --> 00:03:42,990 If you are trying to foresee different error situations and 71 00:03:42,990 --> 00:03:46,770 For every error situation you use a specific exit code 72 00:03:46,770 --> 00:03:48,300 And you provide documentation 73 00:03:48,300 --> 00:03:50,430 About these different exit codes for the user 74 00:03:50,430 --> 00:03:53,523 So that the user can easily find out what went wrong. 75 00:03:55,020 --> 00:03:56,613 Now let's run the script again. 76 00:03:58,417 --> 00:04:00,390 "Script two dot sh hello" 77 00:04:00,390 --> 00:04:02,580 And there we can see the argument is "hello". 78 00:04:02,580 --> 00:04:05,520 So everything is going alright at this point. 79 00:04:05,520 --> 00:04:09,930 Let's have a look at another script, script number three. 80 00:04:09,930 --> 00:04:11,220 So what do we see here? 81 00:04:11,220 --> 00:04:13,080 Well, we see a little bit more complex script 82 00:04:13,080 --> 00:04:16,020 With some new elements that we haven't seen before. 83 00:04:16,020 --> 00:04:18,930 First it starts with "counter dollar one". 84 00:04:18,930 --> 00:04:20,220 That means that the current value 85 00:04:20,220 --> 00:04:22,470 Of dollar one is stored in a variable counter 86 00:04:23,550 --> 00:04:26,370 And next we are going to perform a calculation. 87 00:04:26,370 --> 00:04:28,320 This is a new structure, we haven't seen it before. 88 00:04:28,320 --> 00:04:30,216 The dollar and the double parenthesis 89 00:04:30,216 --> 00:04:32,250 Make for a calculation. 90 00:04:32,250 --> 00:04:34,620 So it takes the current value of counter 91 00:04:34,620 --> 00:04:36,243 And multiplies it by sixty. 92 00:04:37,080 --> 00:04:40,080 So, if I enter "five" for instance 93 00:04:40,080 --> 00:04:43,470 At the moment that this line has been processed 94 00:04:43,470 --> 00:04:45,920 Then the value of counter would be three-hundred. 95 00:04:46,800 --> 00:04:47,880 Now we have a function. 96 00:04:47,880 --> 00:04:50,040 The function name is "minus one" and the purpose 97 00:04:50,040 --> 00:04:52,500 Of a function is to make it easy to refer 98 00:04:52,500 --> 00:04:53,640 To a couple of commands. 99 00:04:53,640 --> 00:04:57,607 So "minus one", and this "minus one" is setting 100 00:04:57,607 --> 00:05:00,990 "Counter two", "counter minus one", and "sleep one". 101 00:05:00,990 --> 00:05:02,220 That's a subtraction. 102 00:05:02,220 --> 00:05:05,130 Every time we call the function, we do a subtraction 103 00:05:05,130 --> 00:05:06,780 On the current value of "counter" 104 00:05:06,780 --> 00:05:07,740 And we are going to "sleep" 105 00:05:07,740 --> 00:05:08,763 For one second. 106 00:05:10,560 --> 00:05:13,380 And then we have the main body of the script 107 00:05:13,380 --> 00:05:15,480 Which is a "while loop". 108 00:05:15,480 --> 00:05:17,817 Now in the while loop we can see the square brackets. 109 00:05:17,817 --> 00:05:20,760 The square brackets are the test command, 110 00:05:20,760 --> 00:05:23,460 While dollar counter greater than zero. 111 00:05:23,460 --> 00:05:24,600 That's what we are looking for. 112 00:05:24,600 --> 00:05:26,253 So as long as the variable counter 113 00:05:26,253 --> 00:05:28,800 is a value greater than zero, 114 00:05:28,800 --> 00:05:32,097 Then we print a message and we run the function "minus one". 115 00:05:32,970 --> 00:05:35,700 Then at a certain moment, this variable counter 116 00:05:35,700 --> 00:05:37,230 Will have the value of zero 117 00:05:37,230 --> 00:05:39,660 And then the script is no longer in the while loop, 118 00:05:39,660 --> 00:05:40,493 It can continue. 119 00:05:40,493 --> 00:05:42,360 So dollar counter is zero. 120 00:05:42,360 --> 00:05:43,830 If dollar counter is zero, 121 00:05:43,830 --> 00:05:46,200 Then we have to double "M percent". 122 00:05:46,200 --> 00:05:48,990 The double M percent is a logical end, 123 00:05:48,990 --> 00:05:51,750 which means that the next command is executed 124 00:05:51,750 --> 00:05:54,570 If the first command was executed successfully. 125 00:05:54,570 --> 00:05:58,350 So this reads as if dollar counter has the value of zero. 126 00:05:58,350 --> 00:06:00,300 Then we print echo time is up, 127 00:06:00,300 --> 00:06:03,120 And then we are run minus one again. 128 00:06:03,120 --> 00:06:06,330 So that will make counter equal to 129 00:06:06,330 --> 00:06:09,360 Minus one and that's what we are testing next. 130 00:06:09,360 --> 00:06:13,050 So if counter is minus one, then it's printing echo. 131 00:06:13,050 --> 00:06:16,380 You are now one second late and it runs minus one again. 132 00:06:16,380 --> 00:06:19,710 And then we have "while true", while true 133 00:06:19,710 --> 00:06:21,000 Will continue forever. 134 00:06:21,000 --> 00:06:23,610 Forever, that means until the script is interrupted 135 00:06:23,610 --> 00:06:26,940 Which can be done by using "control C" key sequence. 136 00:06:26,940 --> 00:06:29,220 So well true, do "echo you are now" 137 00:06:29,220 --> 00:06:34,220 And then we get" dollar counter hash dash seconds late" 138 00:06:34,620 --> 00:06:35,943 Between curly braces. 139 00:06:37,050 --> 00:06:40,170 This part here is a pattern matching operator. 140 00:06:40,170 --> 00:06:42,300 Because we are now in the negative values 141 00:06:42,300 --> 00:06:43,650 And we don't want to script to print 142 00:06:43,650 --> 00:06:45,810 You are now minus three seconds late 143 00:06:45,810 --> 00:06:47,850 Because if you're minus three seconds late 144 00:06:47,850 --> 00:06:50,340 Then you are early and that's why we need to take 145 00:06:50,340 --> 00:06:51,480 Off the minus and that's what we do 146 00:06:51,480 --> 00:06:53,587 With the pattern matching operator 147 00:06:53,587 --> 00:06:56,100 "Hash minus", no stars needed 148 00:06:56,100 --> 00:06:58,560 Because we just want to remove the minus 149 00:06:58,560 --> 00:07:02,910 And this will make the negative value positive and print, 150 00:07:02,910 --> 00:07:07,320 You are now three seconds late and it'll continue forever. 151 00:07:07,320 --> 00:07:08,153 Let's run it. 152 00:07:09,360 --> 00:07:12,835 So I'm running the script with the argument one 153 00:07:12,835 --> 00:07:17,283 So that we can verify that it'll, it'll do one minute. 154 00:07:19,620 --> 00:07:21,243 Alright, so here we go. 155 00:07:22,710 --> 00:07:23,820 Time's up. 156 00:07:23,820 --> 00:07:25,440 You are now one second late, 157 00:07:25,440 --> 00:07:27,300 And this is the infinite loop where we can see 158 00:07:27,300 --> 00:07:29,220 That it correctly displays two seconds 159 00:07:29,220 --> 00:07:32,010 three seconds, four seconds, et cetera. 160 00:07:32,010 --> 00:07:36,150 So if you don't do anything, it'll continue forever. 161 00:07:36,150 --> 00:07:38,250 Fortunately, there is "ctrl C" 162 00:07:38,250 --> 00:07:40,350 which you can use to interrupt the script.