At this point you're almost ready to tackle your first project.
There are just a couple of items left to cover.
That's why in this lesson you're going to learn how to get input from the person executing the script
and how to create an account on a Linux system.
As usual I'm going to start up a terminal on my local system go into our class folder
from there into the project folder that we're working on and we already have created the VM so I'm just
going to run vagrant up to bring up that virtual machine
now will connect to it.
And we'll move into the shared folder of slash vagrant script I'm going to call Ehle user demo of dot
SH So I'll go ahead and start my editor
and I will start out with the show being like we should with every script we write.
Now I'm on a set the goal here or intention for this script.
So as you can see on your screen the goal here is to create an account on the local system and whoever
runs the script will be prompted for the account name and password.
So the first thing you want to do is ask for the user's name.
So I'm just going to create a comment here and leave that as a stub for us to go back and fill in in
just a minute then what we want to do is ask for the real name of the person who is going to be using
this account that is going to be created from there.
We want to get the password.
The next step would be to actually create the user.
And then once we created a user we need to set a password for that user using the password that was
collected earlier.
And finally what we want to do is force that user to change their password the first time they log in.
Up until this point we've been working with scripts that can execute on their own without any external
input or interaction with the person executing the script.
Sometimes you're going to need information from the user in order for the script to do any meaningful
work and this specific example you're going to ask the person who's running the script to supply a user
name and password for the account so that the script can go ahead and create that account.
One way to get input from a user is by using the read shell builtin command which is what we're going
to use in this script.
Another way to get input from a user is to have them supply information on the command line as arguments
to your script and you'll be learning about that method later on in the course.
But for now let's look at the read built in.
So as you already know you can use the type command to tell if a command is a shell built in or if it's
an actual file on the file system would you type read.
It says if you just execute read then you'll get the read shell built in.
Again if you want to see all the options here you can do dash A and the first option which is what's
going to happen when you type in a command is run the shell built in.
Otherwise you can use the path to a file that's on your system and user.
Been read.
So what we want to look at here is the shell built in.
So we're going to use the help built in actually to get help on the read built in.
And I'm going to pipe this into last because I know there will be a good bit of output that probably
comes on the screen.
You can tell what this command does by reading the first line and description it says read a line from
the standard input and split it into fields.
Now let's talk about input for a moment.
There are actually three default types of input and output.
They are standard input standard output and standard error.
By default standard input comes from the keyboard and standard output and standard error are displayed
to the screen and I'm using my words here carefully I'm saying by default because you're going to learn
here in a few minutes that standard input does not always have to come from the keyboard.
But typically that's where standard input comes from.
It also goes on to talk about that the line will be split into fields and so on.
It also says that any leftover words are assigned to the last name.
And if you look at the end here you see name and these three periods are three dots are ellipses here
that says you can specify multiple names.
And what happens is each one of those will assign the value to a variable.
So if you want to assign an entire line of input to one variable just supply one name or one variable
name here at the end of the line.
So that's what we we're going to be using in this script.
Also in our case we want to display some text asking the user to supply us with some standard input
from their keyboard.
So if we go down here and the help you'll see a dash p option and a prompt so we can use dash P followed
by a stream which will act as a prompt came in a get out of this help here by hitting q and let's do
an example at the prompt.
We'll use the read built in with the dash p option which stands for prompt.
Then we're going to specify a string that will act as that prompt.
Here I'm going to put a space at the end of this string so that when the users start typing they're
not typing right at the end of that colon there there's going to be a space.
So now we want to store what they type into a variable and we'll just call this variable thing.
Each i n g and hit enter.
So the read and executes it displays the prompt of type something and now I'm going to provide some
standard input using my keyboard and I'm literally going to type something and hit enter.
Now the value that I typed in there should be stored in the variable thing.
So let's check it out.
Echo
and sure enough something comes back.
Let's do this again we'll just do the same command again and then type something else.
I'm going to type Fred.
So that will echo thing.
And sure enough Fred is return because Fred is the value that stored in the variable named thing.
So this is how you can use the read built in.
And we're going to do this in our script
hey we're going to take the user input that we get and assign it to the variable called user underscore
name.
OK we'll just use this same technique for the real name of the user.
And we're going to assign this input to the variable of comment.
Kate one last read statement here.
We're going to store this standard input into the pass word variable.
At this point in the script we have gathered all the information from the person executing the script
in order to be able to create an account.
So let's go back to the command line here and look at the user add command which we're going to use
to actually create an account.
If you look at the synopsis here on the man page for the user add command it says user add followed
by options which are in brackets which as you know are optional.
And then it has a log in at the end of the command so that is required because it's not in brackets.
Let's talk about logons.
Now the word log in and username are the same thing here in the documentation.
Obviously they're using the term log in when I talk to you or when I'm writing scripts I'll use user
names typically in either case log ins or user names have some rules around them.
Now typically they are eight characters or less by convention.
Now that's not a hard and fast rule but that's typically a convention or a practice that is followed
by the way this is something that you could check in your script if you wanted to.
Or you can just display a warning if the username is over 8 characters and will get the testing for
string sizes in a future lesson.
But that's just something to keep in the back of your mind for now.
So you might be asking well why is there this eight character convention.
The answer is that it's based on history.
That used to be the maximum length back in old UNIX systems and of course Linux was modeled after a
lot of UNIX practices.
And so we started using 8 character usernames and a lot of commands such as P-bass and so on only display
eight characters of username.
So let's jump to the show create a user name that has more than 8 characters and see what happens.
In order to add users to a system you need to use super user privileges.
And one way we can do that is with the sudo command will do Sea-Doo user add.
We're going to leave off all those optional options and then create a long user name.
For example I'm going to use Doug Stamper and hit enter.
So now we can switch to this user again if we can use root privileges we won't have to specify a password.
So I use Su's space dash.
Doug Stamper and hit enter.
By the way the dash option to the su command tells you to start with an environment similar to that
of a real log in and also you can use su Dasch L to get the same experience.
But either way SU Dasch or su dash l will start a log in shell if you will.
So now we're logged in here as Doug stamper user.
We can see our prompt has changed to Doug Stamper.
So now what I want to do is look at the process table with the command POS dash E-F to show every process.
And then you can see here at the bottom that the two commands that are in the process table Bash and
dash.
Yep are by the Doug stamper username but you can only see the first eight characters.
And when the user name is longer than that and then filled you'll see a plus sign after it.
So again it's not something that's going to break anything if you have a nine character or a 10 character
or 20 character username.
It's just something to be aware of that some of these commands such as P.S. are going to give you a
little bit different output because historically user names are limited to eight characters so that's
just something I want to point out here just in this conversation about creating scripts to create user
names.
Going to exit out of Dug's shell here and get back to the vagrant user by the way user names are case
sensitive.
So lowercase Doug is not the same user as capital D lower case o u g.
Again you can do that.
You can use upper case letters but by convention they're in all lowercase.
So it's a good practice to make sure your user names are in all lowercase.
Another thing to know is that you can't include any special characters in your name.
However you can use numbers for example if you're creating accounts using last names and you have several
people at your company that have the last name of Smith.
Then you could do Smith one Smith to Smith 99 and so on and those are all valid usernames.
Let's get back to the man page and I want to point out one more thing to you here.
There's an option to the user add command which is dashi that allows for a comment and that comment
can be any string.
Now generally it's a short description of the log in.
And historically it's been used for a person's name.
So if we make Smith one account and it's for Jane Smith then we put Jane Smith in the comments so we
know what person is associated with that account if the account is not for a person then you can put
in the application that's going to use the account for example.
Another thing I've seen done is when the account gets created you put the name of the user in the comment
field along with the help desk ticket number and that can help for auditing purposes.
For example when you create Jane's account and it has helped us ticket one to three you can go back
and look at helpdesk ticket one two three and said Oh if Jason was a linux administrator that created
that account and then you can see our Jain's manager approved that account and then you can see who
initiated that request from the helpdesk.
So you have full accountability and a full chain of what happened and where that account came from.
OK I know I said there was only one more thing I wanted to show you but it turns out there was another
thing I wanted to show you here and I just thought of it.
It's a dash him option or dash dash create dash home.
By the way when you see that and the man page two options here one a short form dash and another along
form dash dash create dash home.
They both do the same thing and you only need to specify one or the other for speed a lot of times people
use the shorthand name if you want to be super clear you can use long form name the dash dash but we're
just going to stick to dash em here when we use this to create or counts the dash him option here as
it says creates the user's home directory if it does not exist.
It also goes on to say that the files and directories contained in the skeleton directory will be copied
into the home directory.
Traditionally that's ETSI skel S K E L.
But also here it says you can override that default with the dash k option continuing on.
It says By default if this option is not specified and create underscore home it's not enabled.
No home directories are created.
So where is it getting this create underscore home information from.
Well if you look into the configuration section of the man page you're going to find out.
Let's see that now you afford Ford slash for a forward search.
Type in what I'm looking for and hit enter.
And here we are at that section of the man page it says the following configuration variables and the
log index change the behavior of this tool and so that is the Create underscore home variable that we
were talking about with the dash m option.
So actually let's get you to get out of the man page and actually look at that file cat and see log
in deaths and hit enter.
And as you can see they're three quarters away up the top of your screen create underscore.
Home is set to Yes.
So if we were to not specify the dash m option the home directory would get created because that is
the default behavior.
Something just to keep in mind and consider when you're writing shell scripts is that these default
configurations may vary among different systems.
So if you want to ensure for example a user home directory gets created then use the dash in Flag and
then you don't have to depend on this external configuration file.
If you're in a controlled environment where you're sure all the ETSI log in desk files are the same
then you can optionally leave that off.
But if you want to be safe and force the creation of the home directory use the dash him flag and that's
what I recommend.
OK let's get back to our script and use what we learned about these or add command to actually create
a user
so we'll use user add dash see for a comment
dash them to force the creation of a home directory and then give it the user name or log in as it says.
And the man page by the way you may have noticed that I put the comment variable in quotes.
The reason I did that is because this common variable may contain spaces when we prompt them to enter
the name of the person that the account asked for.
Typically people have first and last names if not more names.
So someone will type in Jane space Smith and that will be the information that is stored in that one
comment variable.
But again there's a space.
Now it's important to point out here by putting something in quotes it gets treated as a single argument
or a single item and not separate arguments or separate items.
OK the next thing we need to do is set the password for the user and the command for that is P-A SSW
D.
Now let's get to the shell here and get some information about this command.
By default the password command spelled ph ss d will prompt you to enter a new password for your account
to change your password for another account.
You have to specify that account of course only route can change other people's passwords.
So let's just try the command out here on the command line for our current user type SPSS W.D..
And it prompts us for our current password.
And then if we were to complete this process then we could enter in a new password and our password
would be change.
I'm just going to hit enter here and let it fail so we need to come up with some sort of method to get
around this interactive prompt So let's dig into the main page and see if there is a way to supply the
password non interactively or in some programmatically or automated fashion.
So many W.D..
Now I'm gonna scroll down here dash K for keep.
I don't think we need that.
We're not looking to lock an account which is dash dash dash lock.
Here we go dash dash s t i n this option is used to indicate that P-8s WD should read the new password
from standard input which can be a pipe.
So remember earlier that I said by default standard input comes from a keyboard.
Well standard input can also come from another command when it's used in what's called a pipeline.
When you use a pipe symbol on a command line it means take the standard output from the preceding command.
The command goes before the pipe and pass it as the standard input to the following command or the command
that comes after the pipe.
If the first command displays error messages those will not be passed to the second command.
Those error messages are called standard error output.
You'll learn how to control standard air output later in the course.
So we'll use this dash dash SDD.
An option to set the password.
We also want to force the user to change their password the first time they log in to do that.
We'll use the dash easy option as you can read the description here it says this is a quick way to expire
a password for an account.
The user will be forced to change the password during the next log in a template which is exactly what
we want to do.
OK let's get you to get out of the man page and let's get back to editing our file.
You already know how to use echo to create output.
So we'll use echo to output the provided password and then use that output as standard input to the
P.A. the command.
So echo the password used a pipe which again takes the output of the preceding Command which has echo
takes that output and makes it the standard input for the following command which is a SSW D.
And as we learn by reading the man page we can use the dash dash SDD in option to take standard input
ask the password.
And so now all we have to do is tell the password command what user that this password belongs to.
Finally we need to force the user to change their password at Force log in again as we learned in the
man page we can use the dash e option so a P S S W D E for expire.
User name and then we're good to go.
OK let's exit Oliver shellscript here see if our changes and let's try it out see if it works.
Because this is the first time I'm going to be executing this script.
I need to make sure it has the execute bit set on it.
So I'm going to use the chmod command chmod we can use plus X for example l user Dymo for that SH That
will work.
You can also use the numeric notation of chmod 755.
I'll just use this for variety to show you another way to use the chmod command because we're adding
a user with this script.
We need to run this script with root privileges.
So will you say to all user demo for S.H. and hit enter.
We've been talking about this person named Jane Smith So let's give her an account we'll call it J.
Smith and hit enter the name of the person is Jane Smith and enter.
And the password for the account will use J.
Smith.
One two three.
Now that's not very secure.
But we're going to go with it for now.
The output you see on your screen is from the p SS Dev D command which says changing the password for
user J Smith and then also expiring the password for J Smith.
Now let's switch to the user and use their passwords or do you space dash to act as a log in shell and
then the username J.
Smith and this should prompt for a password because we're not using root privileges or Sea-Doo because
you can do anything they can switch to any account without a password.
But since this is the normal vagrant user that it's going to ask for the account password of this J
Smith account because it doesn't know or doesn't trust this vagrant user.
Hey let's see if our password works.
JAY SMITH One two three.
Hit enter and sure enough we get prompted to change your password so let's change it.
From J.
Smith One two three.
There's something more interesting.
OK obviously I chose a poor password.
Let me try something that is more secure.
OK I've changed my account the first time I've logged in so now the person that created account won't
know their new password.
The password that they're actually using going forward is you can look at the Promp.
We have successfully changed to this account.
Just type exit to log out of the J Smith account and get back to our vagrant account.
Let's quickly recap some of the things we talked about during this lesson.
First off you learned about the read shell built in command which reads one line of standard input and
stores it into a variable.
From there we looked at the user add command and you learned that the Dessie option allows you to specify
a comment for the account you're creating.
Typically this comment will be the user's real name or other relevant information about the account.
You also learned that the dash m option forces the creation of a home directory when creating an account.
Next you learned how to program magically supply a password to the password command by piping the output
of echo into the password command using the dash dash as t in option.
Finally you learn how to force a password reset for an account by using password with the dash option
followed by a username.
At this point you've learnt all the concepts and techniques you'll need to create a script that adds
users to a Linux system.
Next up you get to write that script.