1
00:00:00,000 --> 00:00:10,880
In this video, we'll investigate some advanced scripting options.

2
00:00:10,880 --> 00:00:13,080
So here you can see the example script.

3
00:00:13,080 --> 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,080
Calling it script 3.

5
00:00:22,080 --> 00:00:27,920
And as always, starting with the shebang.

6
00:00:27,920 --> 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,840
And here I'm going to put the value of $1 in a variable with the name counter.

9
00:00:43,840 --> 00:00:44,840
Why do I do that?

10
00:00:44,840 --> 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,639
And you cannot change values of variables that have been provided as an argument.

12
00:00:55,639 --> 00:00:58,599
We call these positional parameters, by the way.

13
00:00:58,599 --> 00:01:01,240
So $1 is the positional parameter.

14
00:01:01,240 --> 00:01:04,980
And the value of $1 was determined while you started the script.

15
00:01:04,980 --> 00:01:10,839
So you can't multiply the value of $1 by 60, which is what I'm doing right here.

16
00:01:10,839 --> 00:01:16,879
By putting your calculation between $ and double parentheses, you perform the calculation.

17
00:01:16,879 --> 00:01:21,040
And here the result will be that counter has a certain value.

18
00:01:21,040 --> 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 $-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:38,879
The function is like an internal command, and it makes writing your script a little

23
00:01:38,879 --> 00:01:39,879
bit easier.

24
00:01:39,879 --> 00:01:49,279
I know beforehand that I am going to need a lot of counter $-1, which is followed by

25
00:01:49,279 --> 00:01:53,239
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 $-1.

28
00:02:00,040 --> 00:02:04,559
And then I'm just calling the function name further on within my script.

29
00:02:04,559 --> 00:02:11,919
Next, I'm using while square brackets $counter minus gt 0.

30
00:02:11,919 --> 00:02:15,880
That's an integer test where I'm going to stay in the loop as long as the value of the

31
00:02:15,880 --> 00:02:20,199
variable counter is greater than the integer value of 0.

32
00:02:20,199 --> 00:02:22,720
Next, we need a do.

33
00:02:22,720 --> 00:02:24,240
And then I'm going to use echo.

34
00:02:24,240 --> 00:02:33,000
You still have $counter seconds left.

35
00:02:33,000 --> 00:02:38,199
And then I'm going to call the function minus 1, because I want to subtract 1 from the current

36
00:02:38,199 --> 00:02:40,679
value of the variable.

37
00:02:40,679 --> 00:02:45,839
And I want to sleep for 1 second so that this script is properly going to count down.

38
00:02:45,839 --> 00:02:50,399
Oops, I just noticed that I have a typo on line 1.

39
00:02:50,399 --> 00:02:51,399
Good.

40
00:02:51,399 --> 00:03:03,399
Next, I am going to use $counter equals 0, double ampersand, echo, time is up, n minus

41
00:03:03,399 --> 00:03:07,039
1.

42
00:03:07,039 --> 00:03:08,759
Couple of things in this line.

43
00:03:08,839 --> 00:03:11,679
First, we have the test counter is 0.

44
00:03:11,679 --> 00:03:16,360
Now, if you have a sharp eye or if you have worked with other scripting languages, you

45
00:03:16,360 --> 00:03:22,839
might have expected $counter minus eq 0, because minus eq is for integers and equals is for

46
00:03:22,839 --> 00:03:23,839
string.

47
00:03:23,839 --> 00:03:29,199
Now, the thing is that in bash scripts, you don't work with variable types.

48
00:03:29,199 --> 00:03:33,279
You can just work with the variable according to its context.

49
00:03:33,279 --> 00:03:39,839
So there is no fundamental difference between $counter equals 0 and $counter minus eq 0,

50
00:03:39,839 --> 00:03:42,639
because it needs to have the value of 0.

51
00:03:42,639 --> 00:03:47,880
And the integer 0 can also be considered as a string 0.

52
00:03:47,880 --> 00:03:52,039
It's not as in Python, where you really need a specific type, and if you address the wrong

53
00:03:52,039 --> 00:03:53,800
type, it fails.

54
00:03:53,800 --> 00:03:55,360
Then we have the double ampersand.

55
00:03:55,360 --> 00:03:58,660
The double ampersand is a logical and.

56
00:03:58,660 --> 00:04:03,539
And the logical and means that echo, time is up, will only be executed if the first

57
00:04:03,539 --> 00:04:05,179
command was true.

58
00:04:05,179 --> 00:04:10,220
So you can read it as if square bracket counter is 0, then echo, time is up.

59
00:04:10,220 --> 00:04:16,140
And then it continues with another double ampersand, which is continuing the minus 1.

60
00:04:16,140 --> 00:04:23,260
These double ampersands or logical ands allow you to write compact if-then-else statements.

61
00:04:23,260 --> 00:04:31,899
Then I need one more, where I'm going to work on a counter is minus 1, double ampersand,

62
00:04:31,899 --> 00:04:42,779
echo, you are now one second late, and double ampersand minus 1.

63
00:04:42,779 --> 00:04:46,940
And then I'm entering a generic loop, while true.

64
00:04:46,940 --> 00:04:51,619
While true is as long as the command true can be executed successfully.

65
00:04:51,619 --> 00:04:56,019
Now the essence of the command true is that it always generates an exit code 0, so it's

66
00:04:56,019 --> 00:04:58,579
always executed successfully.

67
00:04:58,579 --> 00:05:01,179
So always, I'm going to do what?

68
00:05:01,179 --> 00:05:13,179
Well, echo, you are now, and then dollar curly brace, counter, hash, minus, curly brace,

69
00:05:13,179 --> 00:05:15,540
seconds late.

70
00:05:15,540 --> 00:05:18,980
Now what is this, what I'm doing with the counter variable this time?

71
00:05:19,299 --> 00:05:25,260
Well, I'm putting it between curly braces to apply a pattern-matching operator.

72
00:05:25,260 --> 00:05:30,980
And the pattern-matching hash minus is looking for the pattern minus at the beginning of the

73
00:05:30,980 --> 00:05:33,100
string and will remove it.

74
00:05:33,100 --> 00:05:37,579
The purpose of a pattern-matching operator is to remove a pattern.

75
00:05:37,579 --> 00:05:40,100
Now the thing is that we are minus 1 second late.

76
00:05:40,100 --> 00:05:45,179
I don't want to say you are now minus 1 seconds late, because if you are minus 1 seconds late,

77
00:05:45,179 --> 00:05:46,179
you are early.

78
00:05:46,179 --> 00:05:51,660
So the minus needs to be removed, and the pattern-matching operator is how this is done.

79
00:05:51,660 --> 00:05:53,459
Then I'm calling the function again.

80
00:05:53,459 --> 00:05:59,619
I'm closing the script, and we should be OK, and we should be able to run it.

81
00:05:59,619 --> 00:06:06,420
So chmod plus x on script 3, and then let's run it and see what it is doing.

82
00:06:06,420 --> 00:06:12,820
Oh, I don't have an argument, so it starts with a 0, and it immediately prints time is

83
00:06:12,820 --> 00:06:13,820
up.

84
00:06:13,859 --> 00:06:19,339
So I am providing the argument 1, and now you can see that it is nicely counting down.

85
00:06:19,339 --> 00:06:22,059
And this is my favorite countdown timer script.

86
00:06:22,059 --> 00:06:25,859
I use it in courses a lot to tell people how much time left in breaks.

