- The next important part of scripts
That we need to investigate is the conditional statements.
There are different conditional statements that can be used.
"If", "then", "fi" is the first one.
That allows you to execute certain commands
Only if a specific condition is true.
"While" allows you to, to go over a range of something.
Basically as long as a condition is true.
Then you are going to use "while".
"Until" is doing the opposite.
Then we have "case", "case" is where you can use to check
On specific conditions.
And "for" is what you can use
To iterate over a range of items,
To change files for instance.
You will see all of these quite commonly in scripts.
Let me show you a couple of examples.
First, there's a simple example script, "script two"
Which is using "if", "then", "else".
Let me type it and let's analyze.
We need to start with the shebang.
And next, "if", "minus", "Z", "dollar one"
To check if "dollar one" is empty, what is that?
Well, we have the square brackets.
Square brackets are really the test command,
And in this test command we are going to check
And see if "dollar one" is empty.
This can be written in a different way as well.
Just to make sure that you get it,
I've put that in the script.
That will be "if", "dash", "minus", "Z", "dollar one".
Now the amazing thing about the test command,
Let me show you, main test,
Is that you can test on so many items.
So for instance, you can use "minus Z string"
Which is what we are using here to test
If a string is zero, which is the empty string
Or "string one" or "string two", to test
If strings are equal, or "integer one minus eq integer two"
To test if integers are equal.
And I'm just picking out some examples.
Further down you can see tests that you can do
On file types.
So "minus F", if you want to check
If it's a file, or more specifically "minus G" for instance
To check if a file is said group ID.
So there's a lot of things that can be done
With test command.
We are just going to use the "minus Z" option here.
Now what are we going to do if "dollar one" is "minus Z"?
Well then we are going to provide a message.
"Echo you have to provide an argument".
This is also a nice case to use
"Exit", "exit six" for instance.
And next I'm using "fi" to, to quit DF statement.
So only if "dollar one" is empty, then we are
Going to print this message
And we exit with exit code "six".
And in all other cases we are going to use "echo".
The argument is "dollar one"
So let's make this executable and run it.
And there you go, you have to provide an argument.
This is the case where we don't provide an argument.
Now the interesting thing is that
If at this point I'm using "echo dollar question mark"
Then we can see the exit code of the last command
And that's the exit code "six"
The exit code that we have used in the script.
Now this will be particularly useful in bigger scripts
If you are trying to foresee different error situations and
For every error situation you use a specific exit code
And you provide documentation
About these different exit codes for the user
So that the user can easily find out what went wrong.
Now let's run the script again.
"Script two dot sh hello"
And there we can see the argument is "hello".
So everything is going alright at this point.
Let's have a look at another script, script number three.
So what do we see here?
Well, we see a little bit more complex script
With some new elements that we haven't seen before.
First it starts with "counter dollar one".
That means that the current value
Of dollar one is stored in a variable counter
And next we are going to perform a calculation.
This is a new structure, we haven't seen it before.
The dollar and the double parenthesis
Make for a calculation.
So it takes the current value of counter
And multiplies it by sixty.
So, if I enter "five" for instance
At the moment that this line has been processed
Then the value of counter would be three-hundred.
Now we have a function.
The function name is "minus one" and the purpose
Of a function is to make it easy to refer
To a couple of commands.
So "minus one", and this "minus one" is setting
"Counter two", "counter minus one", and "sleep one".
That's a subtraction.
Every time we call the function, we do a subtraction
On the current value of "counter"
And we are going to "sleep"
For one second.
And then we have the main body of the script
Which is a "while loop".
Now in the while loop we can see the square brackets.
The square brackets are the test command,
While dollar counter greater than zero.
That's what we are looking for.
So as long as the variable counter
is a value greater than zero,
Then we print a message and we run the function "minus one".
Then at a certain moment, this variable counter
Will have the value of zero
And then the script is no longer in the while loop,
It can continue.
So dollar counter is zero.
If dollar counter is zero,
Then we have to double "M percent".
The double M percent is a logical end,
which means that the next command is executed
If the first command was executed successfully.
So this reads as if dollar counter has the value of zero.
Then we print echo time is up,
And then we are run minus one again.
So that will make counter equal to
Minus one and that's what we are testing next.
So if counter is minus one, then it's printing echo.
You are now one second late and it runs minus one again.
And then we have "while true", while true
Will continue forever.
Forever, that means until the script is interrupted
Which can be done by using "control C" key sequence.
So well true, do "echo you are now"
And then we get" dollar counter hash dash seconds late"
Between curly braces.
This part here is a pattern matching operator.
Because we are now in the negative values
And we don't want to script to print
You are now minus three seconds late
Because if you're minus three seconds late
Then you are early and that's why we need to take
Off the minus and that's what we do
With the pattern matching operator
"Hash minus", no stars needed
Because we just want to remove the minus
And this will make the negative value positive and print,
You are now three seconds late and it'll continue forever.
Let's run it.
So I'm running the script with the argument one
So that we can verify that it'll, it'll do one minute.
Alright, so here we go.
Time's up.
You are now one second late,
And this is the infinite loop where we can see
That it correctly displays two seconds
three seconds, four seconds, et cetera.
So if you don't do anything, it'll continue forever.
Fortunately, there is "ctrl C"
which you can use to interrupt the script.