All right, a very valuable thing that you can do in a script is the test command.
Tests can be used to test many things, like properties of files, like is a file executable
or is it a file or does it have read permissions, or contents of strings, or values of integers,
and so much more.
And test is commonly used in if-then-else statements.
The common use of test is test condition, and you can also rewrite that as square bracket
condition square bracket, where the opening square bracket is the replacement of the test
command and the opening square bracket needs to be closed by a closing square bracket.
So if statements are very useful in combination with a test, and an if statement is used to
run commands only if a specific condition is true.
And to identify the commands to be run, then is used.
And else can optionally be used to run other commands if the condition is not true.
Now fi must be used to close an if statement.
Let me show you how you can use if statements and tests in a script that makes sense.
So let me create script three.
And in script three, I want to do something with an argument.
So what am I going to do?
Well, I am going to use text is $1.
But if you require an argument, wouldn't it be nice if we test if the argument is available?
Well, we need test in order to do so.
If you want to know more about test, it has an awesome man page.
And in this man page, you can see everything that you want.
And here is the test that I want to do minus Z string.
Minus Z string is what I can use to test if the string is not defined.
And now that we're here, this is a useful test to check if string one is equal to string
two.
We have integers that can be compared.
So integer equal or greater than or less than.
We have file tests, so you can see if a file is newer than or a specific test like file
exists and file has set group ID and so much more.
It's really useful to have a look at the man page of test a bit more closely.
Now what I want to do is if test minus Z $1, then I'm going to print echo.
You need to provide an argument while running this script.
Now that's an error condition, right?
And as it is an error condition, in this error condition, I want to use exit to return an
error code.
Exit codes are normally used by commands to pass information to the parent shell on whether
or not the command was successful.
And the convention is if a command returns an exit code zero, it was successful.
If it returns anything other than zero, it was not successful.
So here I'm using exit nine, and then I'm closing my test using FI.
And for the rest of it, I can keep it very simple.
I'm not even sure if I want to use text is $1, but if ever you want to modify the variable
later on in your script, this might make sense.
Because positional parameters, the $1 and so on, you can't change them in the script.
Other variables can be changed.
That's too advanced for this course, but for that reason, I'm keeping the text is $1 anyway.
And then I'm going to echo the text that was entered is $text.
And now let's run the script again.
Don't forget to make it executable first.
And then I'm running it, and there we can see, oh, I didn't provide an argument.
So it exited.
You need to provide an argument, and it doesn't do the other things.
And now if I do hello, then it is returning the text that was entered is hello.
And I know it's a simple script, but it shows very well how it is working.
And I want to rewrite the script a little bit.
There are two things that I want to change.
First I want to get rid of the test command and rewrite it this way, which is exactly
the same thing as what we have seen before.
So the square bracket notation is a very common alternative for the test notation.
And here we can see that it does work with the argument, and it does not work if you
don't have the argument.
Now you can also use logical operators.
So what is a logical operator?
A logical operator is a command that is only executed based on the result of the command
before.
Now let me use the true command.
True is a command that's always true.
I can use echo dollar question mark to verify the exit code of the true command, which is
zero.
The opposite of that is false.
False is always false.
Echo dollar question mark is showing the exit code one.
Now if I use true, double ampersand, that's a logical end, then we create a logical relation
between the commands, and we only run the second command if the first command was successful.
So let's replace it with false, and that is showing that the second command will never
run.
You can also do the opposite and replace it with a logical or, which is a double pipe,
which means the second command will only run if the first command was not successful,
as you can see.
And it happens to be that these logical operators can be used to replace a simple if-then-else
construction.
So let's rewrite the script a little bit more.
I'm going to put it on top of it, and I make that minus Z dollar one.
So if the test minus Z dollar one is true, then I'm going to use echo, you need to provide
an argument, and another double ampersand, exit nine.
And that makes it, if we don't have an argument that we get out of here, let me delete six
lines using 6DD in VI, and now run the simplified script again.
So script three, here we go, seems to be doing the right thing, and it seems to be working
all right.
And that is how you can use if-then-else together with test as well as logical operators.