In a previous lesson we use the if statement in a positive form meaning if this expression is true then
do something in this lesson we're going to use the logic of if this expression is not true then do something
you're also going to learn how to determine if a given command failed or succeeded by checking the exit
status of the command as well as how to control the exit status of your scripts.
You can follow along with me right now if you'd like or you can wait for the practice exercise where
you'll get to apply all the concepts you've learned so far.
The first thing I'm going to do is open up a command line section of my local machine and move into
our working folder.
I'm here I'm going to go into the project folder that we're working on which is local users.
Now I'm going to start the virtual machine and connect to it.
Now that I'm inside the virtual machine I'm going to C.D into the shared directory of slash vagrant
and I'm going to start working on this script for this lesson which is tell users which stands for a
local user demo.
Oh three got sh horse.
We start out our shell scripts the exact same way every time.
We supply the interpreter to be used on the shebang line here.
Now we're going to specify a goal for our script.
So this script is just going to display the ID and user name of the user that's executing the script.
And we're also going to display to the screen if the user is the vagrant user or not.
So we're going to test for the vagrant user.
I'm going to go ahead and put the steps in as comments and then fill in the code as we go.
So the first step we want to do is display the new ID.
The next thing we're going to do is only display a message if the ID does not match 1000.
From there we're just going to display the username then we're going to test if the username command
succeeded from them and demonstrate how you can use a string tests conditional.
Next we're going to test for not equal or the string.
So let's go back to the top of our script here and start filling in some code.
So the first thing I want to do is display the ID as you already know the U.
ID is a shell variable that is set by default.
So we can just display that we're going to use double quotes because we're going to have a variable
in here that we want to get expanded
Of course when we access a variable we access it with a dollar sign in opening curly brace the variable
name and a closing curly brace it let's just write and quit our changes here and then execute the script
and see what happens.
Remember the script has to have an executable bitset on it before it can be executed so we're going
to chmod the script.
Now we're going to execute it with Dot Ford slash l user demo 0 3.
SH K and displays r you ID which is 1000.
Now let's get back to the script and continue editing our script here.
Now what we want to do is only display a message if the ID does not match 1000.
So we're going to have a variable called you ID to test for.
And we're going to set that to one thousand by the way there is this concept called dry which stands
for.
Don't repeat yourself.
We're going to apply that concept in this script through the use of a variable.
We're going to reference this you id more than once so instead of hard coding that you ID in multiple
places in the script you're going to specify that value in one place.
If you ever want to change that you ID you only have to change it in one place and not go searching
all throughout the entire script and change it in multiple places in a small script like we're writing
here it might not look like that such a big deal but when you create larger more complex scripts you're
going to be glad that you followed the dry principle.
OK so let's test for this you idea with an if statement.
We remember that we use the F word followed by a space double opening brackets followed by a space.
We're going to look at the ID and we're going to see if it's actually not equal to the idea that we're
going to test for
if the ID is not equal to the ID to test for then we're going to display your ID does not match you
ID that we're testing for.
Let's say we don't want the user to go any farther unless they have the ID of 1000 in order to stop
the execution of the script.
We're going to use the exit command by the way we can supply an exit status after the exit command.
We're going to supply one here.
The reason why we're going to specify one is because this script does not successfully execute all the
way to the bottom as it should.
So we're going to want to exit with a non zero status.
So by convention when a script or a program execute successfully it returns an exit status of 0 if it
doesn't execute successfully for whatever reason it returns a non zero exit status.
A lot of times that exit status is one.
However it could be two.
It could be 99 it could be 244.
You can use a variety of different exit statuses if you want.
Let me close out this statement I'll show you an example.
Let me write and quit real quick.
So let's look at a command that we're going to use We're going to be using the user add command to add
users.
So let's take a look at its man page.
I'm going to search for exit status by Ford slash exit and press enter.
So here it's called them exit values the user add command exits with the following values 0 6 success.
You can actually test for these exit statuses and we'll get to that in just a minute.
So for example exit status of nine means the user name is already in use.
So if you're writing a script and you run the user add command and you get an exit status of nine then
you can know that that user name is in use and it's not for some other reason such as an invalid command
syntax which would be an error status of two.
So again we're going to use the exit show built in.
And let's get some help about that.
Let's see what help is available for exit.
So as you can see you can use exit without any options because the in there is in brackets and that
means it's optional.
So you can just type exit and exits the shell if you want to supply an exit status you just supply it
to exit exit 1 for example like we're using in our script.
If you omit that status then the exit status is that of the last command executed.
Sometimes that's what you want and sometimes that's not what you want.
So if you want to be explicit then specify a exit status following the exit shell built in.
Let's get back to our script real quick here.
So now we've gone over the exit status.
So let's review where this dash in e came from.
So if your ID is dash any which means not equal to the ID to test for then we're going to execute this
code.
Hopefully you remember that you can get information about these tests by running help test.
And here we're doing a arithmetic test means we're testing numbers here we have dash Q Which corresponds
to equal dashi and he is not equal Dasch LTE is less than Nashe L E is less than or equal G is greater
than and dash G is greater than or equal to.
So we're using dash any do tests for not equal.
Now let's go ahead and executer script and see what happens.
Says your ID is 1000 and no messages were displayed to the screen because you I.D. does not equal 1000
is actually false.
Now let's see what happens when we execute the script with the different user ID.
Well one way to do that is to execute it with Sulu privileges and that will run the script with root
user privileges.
And as we know the root user always has the UI ID of 0.
So this time you get your you ID is zero your ID does not match 1000.
So the if statement evaluates to if 0 and not equal 1000 then do something which has to echo your ID
does not match 1000 OK continue working on the script.
Now let's display the username
as you probably remember the dollar sign parentheses syntax is used to capture the output of a command.
And we're going to send the output of that command and store it into the variable user underscore name.
Now what we want to do is test if that command succeeded and how we do this of course is with an if
statement.
So Bash has some special variables that it sets.
And one of these special variables is a dollar sign questionmark.
And of course as you know you can encapsulate that and curly braces so dollar sign opening Crilley race
questionmark closing curly brace is actually a special variable that holds the exit status of the most
recently executed command.
So the most recently executed command in our script is ID dash you in.
So the exit status of that script is going to be stored in dollar question mark.
Now we can use dollar question mark to do some comparisons here.
Make a decision.
So if dollar question mark does not equal zero then what we want to do is tell the person that executing
this script that the ID command did not execute successfully and we're going to cause our script to
exit as well as to exit 1 and then close our if statement if we get past this if statement that means
the ID command did successfully complete.
So we can display their username.
So let's execute this script on the command line and also do some demonstrations where the dollar question
marks special variable.
So now the output of the script is your you ideas 1000 your user name is vagrant.
And as you remember the username is a vagrant echo command was directly after the if statement.
So the if statement did not execute.
So that means the ID command actually executed properly.
So let's test this out let's do id dash you in which we know is a good command to get the user name
of the current user.
Again the special variable dollar questionmark contains the exit code.
Let's look at it.
So Will do.
Echo dollar question mark and the exit status of the ID command is zero.
Now let's make the ID command fail in some way I happen to know that dash X is not a valid option to
ID so let's run that command and see what happens.
So we get an invalid option dash x.
So now let's look at the exit status which is stored in dollar question mark.
So now you can see that one is the exit status of that command that failed.
So again by convention it's a non zero exit status and this case is one.
Again it could be two it could be three it could be one hundred thirty nine.
In our user add example that we were looking at earlier.
So again if we want to test for success we'll probably want to test if it does not equal 0 because that's
the convention.
Kate let's continue with our script.
Instead of testing numbers let's do a test against the string.
So let's have a user name to test for
we'll use an IF statement to perform a test.
So this reads if the user name equals the user name to test for if that turns out to be true then what
we want to do is echo your user name matches.
The user name that we're testing for
it's important to point out that the equal sign can either be an assignment operator or a test operator
depending on the context.
If you look at the variable assignment user name to test for that equal sign is used as assignment where
assigning the value of vagrant into the value of user name to test for.
However when we're doing a test inside these double brackets then it's a test operator.
It's not assigning user name to test for to user name it's comparing them it's testing them by the way
sometimes you'll see a double equal sign and let me put this in the script to show you here.
So sometimes you will see that when you use the double equal sign operator the string to the right of
the operator is used as a pattern and pattern matching is performed.
So we're not looking for a pattern we're looking for an exact match so we'll just use one equal sign.
However some people even use the double equal signs when they don't use a different pattern if they're
also using an exact match.
So be aware that sometimes you'll see a single equal sign and sometimes you will see a double equal
sign.
We're going to stick with a single equal sign in this case.
OK let's exit over script and see what happens when we execute it.
Now
those are you ideas.
Thousand are you using name is vagrant and our user name matches Veiga.
Well let's run this with Sue
are you ID is 0.
Your I.D. does not match 1000.
And again if you remember we had an exit statement in our script and we exit it as we want.
So let's check the exit status and see if that's true.
And sure enough the exit status of our script is one.
Hey let's get back to our script and finish it up.
Now let's test for inequality using an if statement with a string.
So we'll do if double brackets
username is not equal to user name to test for.
Then we're going to echo your user name does not match user name to test for and then we're going to
exit with one and we're going to end the statement with ephi.
And finally if we make it all the way to the end of the script we can use exit and we can be very explicit
about the exit status that we want to use we'll use 0 to say our script completed successfully.
Remember that if you don't specify an exit status or exit code the exit status of the most recently
previously executed command will be used as the exit status of your script or as the exit status of
the exit command.
Hey that wraps it up for this script.
Let's save our changes and execute at the command line.
So you can see the output on the screen and let's check our exit status here.
We have an exit status of 0 which means by convention that our script completed successfully.