1 00:00:00,000 --> 00:00:10,920 In this video, we'll investigate some advanced scripting options. 2 00:00:10,920 --> 00:00:13,120 So here you can see the example script. 3 00:00:13,120 --> 00:00:17,160 Let me type it for you so that we can talk about its contents. 4 00:00:17,160 --> 00:00:22,120 Calling it script 3. 5 00:00:22,120 --> 00:00:27,959 And as always, starting with the shebang. 6 00:00:27,959 --> 00:00:31,719 And then I'm defining a variable counter is $1. 7 00:00:31,719 --> 00:00:37,840 As we have seen in the previous script, $1 is the first argument that you are using. 8 00:00:37,840 --> 00:00:43,860 And here I'm going to put the value of $1 in a variable with the name counter. 9 00:00:43,860 --> 00:00:44,860 Why do I do that? 10 00:00:44,860 --> 00:00:49,320 Well, because I want to be able to change the value of the variable. 11 00:00:49,320 --> 00:00:55,680 And you cannot change values of variables that have been provided as an argument. 12 00:00:55,680 --> 00:00:58,639 We call these positional parameters, by the way. 13 00:00:58,639 --> 00:01:01,279 So $1 is a positional parameter. 14 00:01:01,279 --> 00:01:05,019 And the value of $1 was determined while you started the script. 15 00:01:05,019 --> 00:01:10,879 So you can't multiply the value of $1 by 60, which is what I'm doing right here. 16 00:01:10,879 --> 00:01:16,919 By putting your calculation between $ and double parentheses, you perform the calculation. 17 00:01:16,919 --> 00:01:21,080 And here the result will be that counter has a certain value. 18 00:01:21,080 --> 00:01:26,639 And counter times 60 is multiplying it by 60. 19 00:01:26,639 --> 00:01:30,239 Then I'm going to define minus $1. 20 00:01:30,239 --> 00:01:31,239 That's a function. 21 00:01:31,239 --> 00:01:33,839 Now, what is the purpose of a function? 22 00:01:33,839 --> 00:01:36,599 The function is like an internal command. 23 00:01:36,599 --> 00:01:40,040 And it makes writing your script a little bit easier. 24 00:01:40,040 --> 00:01:48,400 I know beforehand that I am going to need a lot of counter minus $1, which is followed 25 00:01:48,400 --> 00:01:53,239 by the command sleep 1. 26 00:01:53,239 --> 00:01:58,400 And in order to make it easy to call these commands, I'm putting them in a function. 27 00:01:58,400 --> 00:02:00,040 So the function name is minus 1. 28 00:02:00,040 --> 00:02:04,599 And then I'm just calling the function name further on within my script. 29 00:02:04,599 --> 00:02:11,919 Next, I'm using well square brackets $counter minus gt0. 30 00:02:11,919 --> 00:02:16,839 That's an integer test where I'm going to iterate over. 31 00:02:16,839 --> 00:02:20,800 That's an integer test where I'm going to stay in the loop as long as the value of the 32 00:02:20,800 --> 00:02:25,160 variable counter is greater than the integer value of 0. 33 00:02:25,160 --> 00:02:27,639 Next, we need a do. 34 00:02:27,639 --> 00:02:29,160 And then I'm going to use echo. 35 00:02:29,160 --> 00:02:37,919 You still have $counter seconds left. 36 00:02:37,919 --> 00:02:43,119 And then I'm going to call the function minus 1 because I want to subtract 1 from the current 37 00:02:43,119 --> 00:02:45,600 value of the variable. 38 00:02:45,600 --> 00:02:50,839 And I want to sleep for 1 second so that this script is properly going to count down. 39 00:02:50,839 --> 00:02:55,240 Oops, I just noticed that I have a typo on line 1. 40 00:02:55,240 --> 00:02:56,240 Good. 41 00:02:56,240 --> 00:03:08,279 Next, I am going to use $counter equals 0, double ampersand, echo, time is up, and minus 42 00:03:08,279 --> 00:03:09,279 1. 43 00:03:09,279 --> 00:03:13,679 A couple of things in this line. 44 00:03:13,759 --> 00:03:16,600 First, we have the test counter is 0. 45 00:03:16,600 --> 00:03:21,279 Now, if you have a sharp eye or if you have worked with other scripting languages, you 46 00:03:21,279 --> 00:03:27,720 might have expected $counter minus eq0 because minus eq is for integers and equals is for 47 00:03:27,720 --> 00:03:28,720 string. 48 00:03:28,720 --> 00:03:34,119 Now, the thing is that in bash scripts, you don't work with variable types. 49 00:03:34,119 --> 00:03:38,199 You can just work with the variable according to its context. 50 00:03:38,199 --> 00:03:44,839 So there is no fundamental difference between $counter equals 0 and $counter minus eq0 because 51 00:03:44,839 --> 00:03:47,559 it needs to have the value of 0. 52 00:03:47,559 --> 00:03:52,800 And the integer 0 can also be considered as a string 0. 53 00:03:52,800 --> 00:03:56,919 It's not as in Python where you really need a specific type, and if you address the wrong 54 00:03:56,919 --> 00:03:58,679 type, it fails. 55 00:03:58,679 --> 00:04:00,240 Then we have the double ampersand. 56 00:04:00,240 --> 00:04:03,559 The double ampersand is a logical and. 57 00:04:03,559 --> 00:04:08,880 And the logical and means that echo time is up will only be executed if the first command 58 00:04:08,880 --> 00:04:10,080 was true. 59 00:04:10,080 --> 00:04:15,880 So you can read it as if square bracket counter is 0, then echo time is up, and then it continues 60 00:04:15,880 --> 00:04:21,040 with another double ampersand, which is continuing the minus 1. 61 00:04:21,040 --> 00:04:28,160 These double ampersands or logical ands allow you to write compact if-then-else statements. 62 00:04:28,160 --> 00:04:37,920 Then I need one more where I'm going to work on a counter is minus 1, double ampersand, 63 00:04:37,920 --> 00:04:47,720 echo you are now one second late, and double ampersand minus 1. 64 00:04:47,720 --> 00:04:51,880 And then I'm entering a generic loop while true. 65 00:04:51,880 --> 00:04:56,519 While true is as long as the command true can be executed successfully. 66 00:04:56,519 --> 00:05:00,920 Now the essence of the command true is that it always generates an exit code 0, so it's 67 00:05:00,920 --> 00:05:03,480 always executed successfully. 68 00:05:03,480 --> 00:05:06,079 So always I'm going to do what? 69 00:05:06,079 --> 00:05:18,119 Well, echo you are now, and then dollar curly brace counter hash minus curly brace seconds 70 00:05:18,119 --> 00:05:20,440 late. 71 00:05:20,440 --> 00:05:23,880 Now what is this, what I'm doing with the counter variable this time? 72 00:05:24,200 --> 00:05:30,160 Well, I'm putting it between curly braces to apply a pattern matching operator. 73 00:05:30,160 --> 00:05:35,880 And the pattern matching hash minus is looking for the pattern minus at the beginning of the 74 00:05:35,880 --> 00:05:38,000 string and will remove it. 75 00:05:38,000 --> 00:05:42,480 The purpose of a pattern matching operator is to remove a pattern. 76 00:05:42,480 --> 00:05:45,000 Now the thing is that we are minus 1 second late. 77 00:05:45,000 --> 00:05:50,079 I don't want to say you are now minus 1 seconds late, because if you are minus 1 seconds late, 78 00:05:50,079 --> 00:05:51,079 you are early. 79 00:05:51,160 --> 00:05:56,559 So the minus needs to be removed, and the pattern matching operator is how this is done. 80 00:05:56,559 --> 00:05:58,359 Then I'm calling the function again. 81 00:05:58,359 --> 00:06:04,519 I'm closing the script, and we should be OK, and we should be able to run it. 82 00:06:04,519 --> 00:06:11,320 So chmod plus x on script 3, and then let's run it and see what it is doing. 83 00:06:11,320 --> 00:06:17,720 Oh, I don't have an argument, so it starts with a 0, and it immediately prints time is 84 00:06:17,720 --> 00:06:18,720 up. 85 00:06:18,760 --> 00:06:23,040 So now I am providing the argument 1, and now you can see that it is nicely counting 86 00:06:23,040 --> 00:06:24,040 down. 87 00:06:24,040 --> 00:06:26,959 And this is my favorite countdown timer script. 88 00:06:26,959 --> 00:06:30,760 I use it in courses a lot to tell people how much time left in breaks.