In this video, we'll investigate some advanced scripting options.
So here you can see the example script.
Let me type it for you so that we can talk about its contents.
Calling it script 3.
And as always, starting with the shebang.
And then I'm defining a variable counter is $1.
As we have seen in the previous script, $1 is the first argument that you are using.
And here I'm going to put the value of $1 in a variable with the name counter.
Why do I do that?
Well, because I want to be able to change the value of the variable.
And you cannot change values of variables that have been provided as an argument.
We call these positional parameters, by the way.
So $1 is a positional parameter.
And the value of $1 was determined while you started the script.
So you can't multiply the value of $1 by 60, which is what I'm doing right here.
By putting your calculation between $ and double parentheses, you perform the calculation.
And here the result will be that counter has a certain value.
And counter times 60 is multiplying it by 60.
Then I'm going to define minus $1.
That's a function.
Now, what is the purpose of a function?
The function is like an internal command.
And it makes writing your script a little bit easier.
I know beforehand that I am going to need a lot of counter minus $1, which is followed
by the command sleep 1.
And in order to make it easy to call these commands, I'm putting them in a function.
So the function name is minus 1.
And then I'm just calling the function name further on within my script.
Next, I'm using well square brackets $counter minus gt0.
That's an integer test where I'm going to iterate over.
That's an integer test where I'm going to stay in the loop as long as the value of the
variable counter is greater than the integer value of 0.
Next, we need a do.
And then I'm going to use echo.
You still have $counter seconds left.
And then I'm going to call the function minus 1 because I want to subtract 1 from the current
value of the variable.
And I want to sleep for 1 second so that this script is properly going to count down.
Oops, I just noticed that I have a typo on line 1.
Good.
Next, I am going to use $counter equals 0, double ampersand, echo, time is up, and minus
1.
A couple of things in this line.
First, we have the test counter is 0.
Now, if you have a sharp eye or if you have worked with other scripting languages, you
might have expected $counter minus eq0 because minus eq is for integers and equals is for
string.
Now, the thing is that in bash scripts, you don't work with variable types.
You can just work with the variable according to its context.
So there is no fundamental difference between $counter equals 0 and $counter minus eq0 because
it needs to have the value of 0.
And the integer 0 can also be considered as a string 0.
It's not as in Python where you really need a specific type, and if you address the wrong
type, it fails.
Then we have the double ampersand.
The double ampersand is a logical and.
And the logical and means that echo time is up will only be executed if the first command
was true.
So you can read it as if square bracket counter is 0, then echo time is up, and then it continues
with another double ampersand, which is continuing the minus 1.
These double ampersands or logical ands allow you to write compact if-then-else statements.
Then I need one more where I'm going to work on a counter is minus 1, double ampersand,
echo you are now one second late, and double ampersand minus 1.
And then I'm entering a generic loop while true.
While true is as long as the command true can be executed successfully.
Now the essence of the command true is that it always generates an exit code 0, so it's
always executed successfully.
So always I'm going to do what?
Well, echo you are now, and then dollar curly brace counter hash minus curly brace seconds
late.
Now what is this, what I'm doing with the counter variable this time?
Well, I'm putting it between curly braces to apply a pattern matching operator.
And the pattern matching hash minus is looking for the pattern minus at the beginning of the
string and will remove it.
The purpose of a pattern matching operator is to remove a pattern.
Now the thing is that we are minus 1 second late.
I don't want to say you are now minus 1 seconds late, because if you are minus 1 seconds late,
you are early.
So the minus needs to be removed, and the pattern matching operator is how this is done.
Then I'm calling the function again.
I'm closing the script, and we should be OK, and we should be able to run it.
So chmod plus x on script 3, and then let's run it and see what it is doing.
Oh, I don't have an argument, so it starts with a 0, and it immediately prints time is
up.
So now I am providing the argument 1, and now you can see that it is nicely counting
down.
And this is my favorite countdown timer script.
I use it in courses a lot to tell people how much time left in breaks.