Welcome to the exercise walk through.
Now the goal of this particular exercise is to create a shell script that adds users to the same Linux
system as that shellscript was executed on.
This exercise actually builds upon the previous exercise.
It's going to be very similar.
We're just going to really make a few improvements.
And by the way that first script you created in our scenario has been used by the helpdesk team to create
accounts and they are really happy about it.
But after using it for a little while they have some requests they would like you to make some changes
to it.
When you get a chance and of course you're so nice that you're going to oblige one of the requests they
have is that they're kind of tired of coming up with passwords every time they create account and they
think you could automate that for them just generate a password for the account so that they don't have
to do that.
Another thing that they found when using that particular script was that they already knew all the information
that they were going to provide the scrip.
However they would have to wait to be prompted in order to type in that information.
They thought it would be just a little bit more efficient if they could just type everything right in
at the command line all at once and not wait to be prompted.
So again you go back to thinking about the requirements what the script should do.
And a lot of these requirements are going to be exactly the same as you had in the first exercise.
The first thing that's a little bit different is that you're going to name the script something else
just to distinguish it from the previous script so that they know they're running the new script and
not the old script so you decide to call this script add dash new dash local dash user data.
Sh like your previous script it's going to enforce that it be executed with super user or root privileges.
And if the script is not executed with super user privileges it will not attempt to create a user and
simply return an exit status of one.
Here's something you decide to add to the script that wasn't requested by the helpdesk team but you
can see how this would be helpful.
And that thing is to provide a usage statement much like you would find in a man page if the user executing
the script does not supply an account name on the command line.
And if they do that you teach them how to use the script.
This script again is a little different than the last one.
So maybe they're used to getting prompted But this script requires them to have a user name.
So you're going to tell them about that.
And when you tell them about that you're also going to exit your script with a status of one.
Now speaking of supplying things on the command line you decide that the first argument provided on
the command line should be the username for the account and any additional remaining arguments on the
command line will be treated as the comment for the account.
And that's pretty smart because the account is always going to be one word or one item and the description
could be nothing.
Or it could be a name like Jane Smith or it could be a description of an application for which the account
is being created.
The next requirement you've listed is to generate a password for that new account.
And again you're not going to proffer this password you're just going to create it inside the script
just like in your previous script you're going to inform the user if the account you're trying to create
was not able to be created for some reason.
And in that particular case you're going to exit the script with an exit status of one family you're
going to display the user name the password and the host where the account was created.
This is just like in your previous script.
So at this point let's go ahead and use the existing virtual machine we used in the first exercise.
So we'll move into that vagrant project folder.
Now we just run vagrant up to start the virtual machine.
Now connect to it with vagrant S-sh once you're connected you can move into the shared folder of four
slash vagrant.
At this point you can either create the script inside the virtual machine using your favorite editor
beat them nano or I-Max or you can create this script on your local machine using your favorite editor
there.
But if you do that just remember to place your script in the shared folder which you'll be able to access
here on the virtual machine and the forward slash vagrant folder.
I'm going to use the vim editor and just create that here on the local machine so I'm going to type
them add new local user dot sh.
Of course it goes without saying that every script starts with a Shabak and then we're just going to
provide some information a header if you will for this script.
So here we're just saying that the script creates a new user on a local machine and that the first thing
on the command line after the script has to be a user name and anything that comes after the user name
will be considered a comment for the account.
Next we're going to automatically generate a password.
And finally we're going to display that information.
Perhaps that's a bit verbose for a header but you can use this or your own language or even use the
usage statement that we're going to come up with here in just a minute.
So of course we really don't want to do anything unless the user that's executing the script is doing
it with proper permissions.
There's no reason to try to add a user account when you don't have root permissions.
So let's check for that first.
And again this is exactly the same thing you did in your first script.
So if a user ID is not equal to zero.
And that means the user is not root.
And then we'll exit with an exit status of one and close out or if statement here.
Now the next thing we want to do is make sure that they supply at least one argument.
It's required that they give us a username so we'll go ahead and check for that next.
So if the number of arguments is less than one then you can do this you can give them a usage statement
and just echo back to them the command that they just executed.
Tell them that the user name is required.
And you're going to put a comment in brackets here because that's optional and there could be more than
one comment or more than one word.
And so to repeat that you just signify that with three dots here and then close the echo command.
Now you don't have to use this exact statement.
You could have come up with something on your own and you could have written this in paragraph form.
But if you are used to reading man pages and built in help with bash you'll see this very same thing
this very same format.
So there's no use in reinventing the wheel here.
We'll just go ahead and use the same thing that we see everywhere else.
Again if you come up with something different that's totally fine.
Obviously it works.
You know this is not going to affect the execution of the program.
We'll give them some more information here.
And then we'll exit with one because the script didn't complete.
And now that we're sure that they have provided a user name we can go ahead and assign that user name
to a variable.
So I'll just use upper case user underscore name and assign the value of whatever is in the first positional
parameter to that variable.
Now as you already know our requirements in the Help statement that we just wrote here that everything
else on the line are considered comments
since we've already assigned a user name to the value that was stored in the first positional parameter
dollar on one there.
We no longer need dollar sign one.
So what we can do is use the shift command to shift everything down one more or less making dollar sign
1 drop off or get chopped off.
And then what is left obviously are the comments.
So that means we can do this we can assign the variable comment to every thing else now remember dollar
sign at sign is every positional parameter starting at 1 and beyond.
So this captures everything that's left on the command line.
Again running shift took the user name out of the positional parameters and then again what's left is
going to be the comments.
Now what you want to do is generate a password
will assign this to the password variable.
Again you could use any variable name that you would like.
That's a valid variable name.
This is just what I happened to come up with.
Now we're going to use command substitution starting that with a dollar sign and then an open parentheses.
And then here is where you'll put the command that will generate a password and previous lessons.
We went over a few different methods and you just simply can pick one your favorite one or the one you
think makes most sense and use it here.
So you don't have to use this exact method to generate this password either.
I'm going to create a forty eight character password so this is how I'll do it here.
Now we have all the information we need.
We got the username from the command line.
We also got the comments from the command line.
We didn't ask for a password.
We went ahead and generated on our own.
And so now we're ready to create the account.
The dash C stands for comment and so will supply the comments to the dash see argument here.
And we're going to put those comments in quotation marks so that they are treated as one thing one argument
to the dash see option.
Now we can use dash them to force the creation of a home directory.
And now the only thing we have left is supply is the user name.
At this point what we want to do is check to see if the user and command succeeded.
Of course if it didn't succeed for some reason we don't want to tell the user that hey we created an
account when we actually didn't.
So we'll want to exit out of our script as well.
So what we can do here is check for the return status
so of dollar sign questionmark which contains the exit status or return status of the most recently
executed command.
If that does not equal zero then something went wrong.
So then what we'll say is
the account cannot be created and we are going to exit with a non-zero exit status as well.
Now we need to set the password on the account
and we'll just echo the password into the password command with the standard in option and then we'll
supply the user name that that password is for.
So again the output of the command that precedes the pipe that comes before the pipe is used as the
standard input of the command that comes after the pipe which in this case is password.
And we're telling it to read standard and with dash dash SDD i n.
Let's check to see if the password command succeeded as well.
Well just tell the user that the password couldn't be set and then exit with one.
The last thing we need to do is to force a password change on first log in for the account.
So we'll do this force password change in.
Now we're just going to display the information.
Print out a blank line because we know those password commands generate some output so we just separate
them with a blank line and then we'll display the username
another blank line.
And will display the hostname name which is a built in variable in bash.
We've made it to the end here obviously the script succeeded.
So we're going to exit with an exit status of zero.
Now it's time to save our changes.
Now remember to set the permissions on the script before you try to execute it.
So we'll do that now with chmod 755 add new local user data sh.
Now let's go ahead and test the script.
Of course we're creating accounts so we need root privileges.
And one way to do that is to use the sudo command so will you Sea-Doo and then will you stop forward
slash and the name of the script.
By the way I'm using tab completion here I just typed in the first few letters of the command name there
and then hit tab and I let bash complete that for me so I don't think I'm a super fast typer.
I'm just using some shortcuts and that's when you probably already know.
But just to be clear here that's what I'm doing.
So now what we need to do is supply a user name.
So we'll create the account name of J L O C K E.
And then what we can do here is provide some comments and one common comment for a user account is obviously
the user name that we'll be using it the person's name.
So we'll use John Locke.
That's the person that's going to be using this account and we'll hit enter to go ahead and create this
account.
Hey that appears to have worked.
Let's go ahead and create a couple of other accounts and then check out those accounts.
So let's do this.
It's too soon to add local users.
Let's use the Russell
it ener.
Great.
We get a new password for this account doesn't match the other password.
As a matter of fact the password that you're going to see will not match either because it's being generated
on the fly.
It will do one more account here.
We'll make this say an application account so those two people are philosopher names all pretend that
this is going to be a philosophy application of some sort.
And so we'll just call this Phil app and then we'll just use philosophy.
Application user as the account description or the comment.
Now we've created three accounts so there should be three new entries in the ETSI password file.
So one way to view the bottom portion of a file is with the tail command the tail command is just more
or less the opposite of the head command which you're already familiar with.
So I'm just going to use tail Desch 3 at CPA.
SS WD sure enough the first account we created has the proper username.
John Locke as the comment the same with the second one and the third account we created the application
account add three words as the comment and all three of those words have ended up in the password file.
So it appears that our script is working as planned by the way you may see different account numbers
depending on what accounts are on your system.
For example your jay like user may have an idea of 1004 or 2000 or some other account depending on what
you've previously done on the system and what other accounts exist and so on.
So don't be alarmed if it's not 100 percent exactly the same on your system as what is being displayed
here.
Let's go ahead and switch to an account and make sure that it's forcing a password change on log in.
So I'm actually going to scroll up to find the password for the Jay lock account.
So the user name Jay lock and here's this big long password.
I'm going to copy it and then I'm going to go down here and switch user to the Jay lock account.
Now it asked me for the password.
I'm going to paste it in and ask me for the current password.
And now it's asking me for a new password so what it indeed is changing the password on first log.
So let's use something here.
OK.
I'm successfully switched to the J lock account.
You can see it's A-J lock at local users so that looks right for a password change.
When we first use the account so everything is looking good so I'm just going to type and exit to get
back to the vagrant user.
Now we want to make sure that the script exits with a non-zero exit status if the user does not use
super user privileges.
So let's just execute this command with out using SU to hit enter.
And it says Please run with suruh or as root and then so we can check the exit status with dollar sign
questionmark.
Obviously you can enclose that questionmark in curly braces but I'll just show you that you also don't
have to do that so I'll just give you some variety here.
So sure enough one is not zero.
So that is exactly what we're looking for.
And as a matter of fact one is what we supplied as the exit code for the exit statement.
Now let's check for the other error condition which is where executing the script as true but we didn't
give a user name.
So let's try that.
What do you say to add new local users data SH and hit enter.
And sure enough it spits out our information here to teach the user how to use it.
It tells them that they have to supply a username or otherwise this script is not going to work.
And again we'll look at the exit status and sure enough it's one so everything is working as planned.
All right.
I'm done with this script and I'm actually done for the day so I'm going to log out of the vagrant virtual
machine and I'm actually going to hold it.