In this lesson you're going to learn about some special variables created by the shell at a store the
output of a command in a variable.
And we're going to talk a little bit about the if statement first thing I'm going to do here is open
up a terminal on my local machine.
I'm going to go into our class folder of show class room here.
I'm going to go into a local user CD local users.
We're still working on creating local users and I'm going to start this virtual machine
once the machine is booted you can connect to it with vagrant S-sh.
Now let's move into our shared folder of Ford slash vagrant Tim get there with a cd command.
Now let's start working on the script for this lesson Minick called this L user demo to dot S H.
Remember we always start our scripts so the banks are going to start out with a sharp pain or spin or
slash Bash.
As you probably remember from the last lesson I like to put a header at the top of the file that just
describes what the shell script is going to do.
I'm going to put that in right now.
So these are the two main goals that I have for this script which is to display the idea which stands
for the user id or user identification number and the user name of the user that's executing this particular
script.
And the last thing we want to do in this script is to display if the user that is executing the script
is the root user or if they are some other user they're either the root user executing the script or
they're not.
One of the things that can help you write shell scripts is to think about all the steps that it takes
to accomplish your goal.
We have the goal in the header.
So now let's think about some steps that we need to take in order to accomplish our goal.
A lot of people call this pseudo coding.
And so what you do is you just write in plain English and your language of what you want to accomplish
as a comment.
But those comments in your file and then go back and put in the exact shell commands and syntax that
you'll need to accomplish.
Each one of those tasks so it's kind of like outlining before you write the shell script.
So I'm going to go ahead and do that now the first thing I want to do is display the ID.
The next thing I want to do is display a user name.
And finally what I want to do is display if the user is the root user or not.
So now we have our Hetter and our pseudo code now and start filling in the code.
So the first thing we want to do is display the ID.
And of course you know that the echo shell built in displays information to the screen and we're going
to use something that the shell gives us for free I might show you here your ID is you ID as you know
if you perceive something with a dollar sign and enclose it in curly braces that it's a variable.
This variable is you.
But we didn't assign a variable or set a variable.
So where is that variable going to come from.
Well remember that I said you get a lot of stuff quote stuff for free.
When you use bash one of the things you get for free are special preset variables.
So let's go ahead and write and quit our script.
Remember before you execute the script for the first time you want to set the executable bit on it.
And now let's execute our script and see what happens.
Hey it says your ID is 1000.
So let's look at the bash man page to see where this UAD comes from.
By the way to navigate a man page you're going to use vi or VM keybindings.
And so how to search for it is a forward slash and then the string you're searching for.
We're looking for you.
So we'll do that and hit enter to take us to the first instance of that.
And if we want to keep going down the file it in to go to the next occurrence.
And here you can see that the information about this says that it expands to the ID of the current user
initialized at Shell startup.
This variable is a read only.
That means that a user can't change it.
So if we were to check against this ID we can be fairly certain that it's going to be the actual source
of truth.
It's really going to have the you ID in it.
Now let me back up a little bit.
I'm going to hit shift and to back up to the previous match.
Now there is a similar variable here called the you I.D. which expands to the effective user id of the
current user initialized at Shell startup.
This very well is also read only.
So in reality we could use EU ID or ID.
The only time that you ID would be different from EU ID is in the case of a set id script when a file
has a special set id permissions set on it.
The file actually gets executed as the owner of the file no matter what user is executing the file.
So if its set you ID root and where the vagrant user and we execute the script are you ID is 1004 vagrant.
But r e ID is 0 for root.
However this is rarely seen modern Linux systems for security reasons do not allow scripts to be set.
You ID.
So this is just something to keep in mind for example if you're working on say a proprietary UNIX system
that's somewhat old.
But again if you're working on modern day Linux systems you can safely use you ID because e you ID and
you ID are going to be the same but just know if you see that somewhere else that is where that comes
from.
OK let's back up even farther Monotype questionmark for a reverse search and type shell and hit enter
here.
So the ID and you ID that we were just reading about happened to be in the shell variables section of
the bash man page.
So not only do you get e ID and you ID you get several other variables that anytime you start Bash that
all these are going to be set.
So if you want take a moment here and just read down these variables and see what's available to you.
OK I'm just going to hit Q to quit since we're done looking at the man page and I quickly want to demonstrate
that to you ID is indeed read only.
So if we do echo your I.D. It's 1000.
If we do you ID equals let's say we're going to set it to 1000 of one hit enter.
It says no you can't do that because it's a read only variable.
So again we can trust the information that's in the ID variable.
So if you remember from our script the next thing we want to do is actually display the user name.
So how do we do that.
Well one way to do that is with the ID command and then see this type dash a ID.
So it is actually a program and it's not a shell built in.
So since it's not a shell built in we can learn about it by using the man page man id by the way when
you're looking at a man page.
If you see things that are enclosed in square brackets that means they are optional.
So here you can see ID with optional option an optional user.
So actually you can run this command on its own and doesn't require any arguments because they're all
optional.
Additionally if you see these three dots or three periods also called an ellipsis I do believe that
you can specify multiple of whatever precedes it.
So you can have the command followed by multiple options and then one optional user.
So the description here says print user and group information for the specified user or when user omitted
for the current user.
So I'm going to look at what our options are here dash and we'll print a name instead of a number.
And so we are actually looking to print a name so that's useful that you print only the effective user
ID.
So those look like the two things that we need to use to print a user name would be Dasch you in the
queue here and just test out the ID command with no options ID it enter and says Are you ID is 1000
vagrant.
Is our user name.
The idea is 1000 which is a group ID and vagrant is the username of that group and the groups that were
member of is also called vagrant with a group id of 1000.
So now let's specify ideaspace dash you and that prints out the ID of 1000.
Now if we wanted to add the dash in option we could do this ID dash you dash and and it displays our
user name.
Also want to point out that it doesn't matter about the order of these options.
So I can do id dash and DSU and get the same result.
Another thing I want to point out is when you have these single character options you can combine them.
So will do id dash.
And then we can list all the single character options that we need without any for the dashes.
So we'll do dash and you and that will give us our user name or it doesn't matter about the order so
we can do id space dash you and press enter and we get the same result.
For me Id dash you in is a steadily pleasing because it's you are a user and for user name so that makes
sense to me that's the command I'm going to use.
But any of those variations work obviously so if you see them you'll know what's going on.
By the way some of you may be familiar with the Who am I command will execute it now.
And that would be another option.
Let's look at what type of command it is.
It is a program so I'm going to use the man page to learn more about it.
Who am I.
Print effective user ID.
Who am I.
Again you can specify options.
The options are optional and you can specify more than one of them.
The description is prent the user name associated with the current effective user id same as ID dash
you in.
So that's what we're going to use ID dashi when you can use.
Who am I.
If you would like as well hey let's return to editing the script.
Before we display the user name let's assign the user name to a variable we'll call that variable user
underscore name.
If you want to store the output of a command into a variable you use the syntax you see here on your
screen which is a variable name equals dollar sign opening parentheses.
Then the command and the clothing parentheses the value for the variable name user underscore name will
equal the output of ID dash you in.
Let's test this here let's echo it to our screen.
Him to say my changes and execute the script
so it says your ID is 1000.
Your user name is vagrant want to demonstrate an alternative syntax and you'll see sometimes as well.
So let's edit our script let's comment out this line.
I'm going to make a copy of it.
And then instead of dollar sign opening parentheses I'm going to replace that with a tick and the closing
parentheses I'm going to replace that with a tick as well going to execute the script and see if we
get the same result.
And sure enough we do let's get back into here.
So that is an older style syntax.
The tick marks that surround a command to assign that to a variable that's the Oler syntax.
So again I like to show you these things because different people write shell scripts in different ways
and you'll be looking at Shell scripts from perhaps earlier times that were written without these modern
conventions so if you see these back Tick's know that it's the same thing as the dollar sign parentheses
syntax.
So going to remove that line uncomment that other line and we should be back to where we were.
For someone to type out an if statement here and then we'll talk through it.
So an if statement begins with the key word if followed by a space then double opening brackets followed
by a space and then an expression after that expression you'll use a space and then the closing double
brackets on the next line you'll use the keyword then and then whatever follows that will get executed
if the expression evaluates to true.
So in our case if the ID is equal to zero then we'll execute the code that says echo you are root.
If you want to include an else statement then you use the keyword and then the code that follows that
will execute.
If the expression evaluates to false so if ID is not equal to zero then what's going to happen is Echo
will be run with you are not root displaying to the screen you close your if statement with F I which
is the reverse of I-F.
And that is the general syntax for the if statement.
I want to point out a couple of different things here again.
The F word should be followed by a space.
The opening brackets should be followed by a space and the closing double bracket should have a space
preceding it.
When you are using variables inside of these double brackets as a test you always want to enclose them
in quotation marks.
OK let's get back to the shell and learn a little bit more about this statement.
So let's do type dash a.
If it turns out that it is a shell keyword so we can get help on that with help.
I have.
So here it says if commands semi-colon then commands a semi-colon and then as you already know things
that are in brackets are optional.
You can absolutely use Elif or else.
And then what is not optional is the closing f I.
So if you remember I said we were going to do this we're going to use F and then double brackets here
and do a test.
So what I want to point out is in the if description it says if commands a semi-colon then commands
a semi-colon what the semi-colon is.
Is the command separator another command separator is to hit enter or begin on a new line.
So if we bring this last command in our shell history back up I'll just hit the up arrow you can see
that where there were line breaks when I entered them in on the command line.
They are now replaced with semi-colon so again the semi-colon is a command separator so any time you
see a semi-colon in some of these descriptions.
Know that you can use a new line and I like to use the new line so it doesn't run altogether and it
looks visually better to me and it's easier for me to read.
However it is correct to also use a semi-colon and keep it all on one line.
I just want to point out that if your command is not completed when you hit enter you'll get a different
looking prompt here which is the greater than sign.
And then once the command is done then you'll get your normal prompt back.
I just wanted to point that out.
So let's keep reading about the above statement.
The IF command list is executed you can think a list is a list of commands.
If the IF commands list is executed if its exit status is zero then the then commands list is executed.
You can think of an exit status of 0 being true.
So if this is true then DO THAT else.
If it's not true then do something else.
So you have a general idea of how the statement works.
However in the if description it says if commands and if those commands have an exit status zero then
execute some code.
Well where do we get these double brackets.
Well let me show you those double brackets are actually a shell keyword so let's get help on those.
So here it says double brackets expression closing double brackets also says it returns a status of
0 or 1 depending on the evaluation of the conditional expression expression.
Expressions are composed of the same primary is used by the test built in and may be combined using
the following operators.
We're not going to worry about those operators for a moment.
But the thing to keep in mind here is that it says you can use the same things that you can use with
a test built in to generate your expressions.
So
test is indeed a shell built in as well as a command.
We're interested in using the built in so we'll get help with it.
Will do help test and I know from experience that a lot of information is going to scroll off the screen
so one way to prevent that is to use a pager so will use a pipe to pipe the output of help test into
our pager of Lessel Let's hit and and see what happens.
So the test a built in evaluates conditional expressions.
Here again to just reiterate what you already know 0 is true one is false.
So here are all the tests that you can do.
You can do file operators like Dasch a file to test if a file exists Dasch be if its a block special
device and so on Dashti if its a directory.
So there are all these types of tests you can use against files.
Additionally there are string operators so dash Z string returns true if the string is empty or Dasch
in string returns true.
The string is not empty.
You can compare strings with the equal sign or the not equal that less than and greater than etc..
This is actually what we're doing in our shellscript We're comparing the number of the ID to zero word
saying is the ID dash Q Which stands for equal to zero.
If it is equal then that's true and it returns a zero Q to exit out of the pager.
OK let's execute our script and see what result we get.
Says here you ideas 1000.
Your user name is vagrant.
You are not root.
By the way if I felt the mention of the idea of zero is always assigned to the root user root always
has the ID of 0 on every Linux system that you'll ever encounter.
Every Unix system that you'll ever encounter route is always the idea of zero.
That's why we checked against it because it's a guarantee.
You could in theory test against the username but it's probably a little bit more safe and secure to
test against ID zero.
Because again that is always 100 percent always assigned to the root user.
Speaking a root let's execute this program as the root user.
One way to do that is use the command which stands for super user.
Do so we'll do Sudoku.
No user demo or to SH and hit enter.
So now since we're using the sudo command this command actually gets executed as root.
So it says your ID is 0.
Your user name is root you are root.
We could have also switched to the root user and done this as well.
Can do su.
And by the way the password for the root user on this system is vagrant and then now we can execute
that as the root user and it says here you ID zero your username as root.
You are definitely root.
OK I'm going to exit back out to the vagrant user.
Let's look at the contents of our file.
One more time.
I want to point out that this double bracket syntax we're using with the if statement is a bash specific
thing.
So this may not be portable to all shells.
For example if you use NCSA script that's probably not going to work for you.
But if you're using bash it will work.
Another thing to note about the double bracket syntax is that it really replace the single bracket syntax.
So actually you may see some shell scripts that look like this.
If with a single opening bracket then a conditional expression and a single closing bracket.
So again just know that the double brackets is the new way to do it.
You can also use the single brackets that's an older way to do it.
Let's get out to the shell here and we can actually do this.
The single opening bracket is a shell built in but it's actually also a command on the file system.
So on older Unix systems this is how it would work.
So sure enough that's a file that's located at user bin.
Opening bracket it's a file and you can execute it like that.
So if you're using a really slimmed down shell that doesn't have the opening bracket built in the shell
script will default to using user bin and the opening bracket.
However we're using bash We don't have to worry about that.
And again we're going to use the newer syntax of the double brackets OK let's look at our script one
last time.
Now this last a block of code.
This if statement.
If the UI equals zero then you are root.
Else you are not root.
Some people call this type of checking idiot checking and it's to help people from doing idiotic things
other people call it sanity checking.
And by the way it's a good idea to check for any requirements in your shellscript if you know something
that is required for the script to work properly check for it.
Never assume things are just going to be the way that you want them to be.
For example if a script adds users and adding users requires root privileges then you want to check
for root privileges.
Don't assume that the user knows that they need to execute it with root privileges.
They may executed as their own user.
So you want to do some sanity checking or some idiot checking if you will to make sure that that happens
as needed.
And you want to do this type of sanity checking throughout the script whenever it's needed.
Hey that brings us to the end of this lesson where you learned about some special variables that are
created by the show.
Now you can look up those variables by using man bash.
You also learned how to store the output of a command in a variable using the dollar sign parentheses
syntax.
You also learned about how to use an if statement or and our specific case the if else statement and
we talked a little bit about conditionals.
You also learned that you could use help test from the command line to get some information about possible
tests or expressions or conditions that you can check for.
Finally you learned about some of the older syntax options such as using back text when assigning the
output of a command to a variable and using a single bracket when doing tests versus the double bracket.