Welcome to this exercise walk through now the script you're going to write today is very closely based
on the previous script you wrote.
As a matter of fact we don't have to do any new coding if we don't want to.
You can simply update that existing script to conform to the new requirements that you have now.
And speaking of those new requirements the helpdesk in our scenario has been using your script and they
notice that there are some extra output that they don't need that they don't care to see.
It really doesn't make any sense to them.
So they want you to make that output disappear.
And of course you know that output comes from the password command.
Ss.
And that you're going to have to use IO redirection to redirect that output to the null device.
And so you start to think about what else you can do to this script with IO redirection and you come
up with that you could actually conform to standard conventions standard Linux programming conventions
by redirecting error messages to standard error and so on.
So with that in mind let's look at just a couple of changes that we're going to make with this script.
So we're going to change the name of the script from add new local user to add newer local user.
Now in the real world I wouldn't necessarily change the name I would just make these couple of updates
to that script in place of course tested first and then put it in for action.
However just for the sake of distinguishing what exercise in what script you're working on if you need
help and so on.
I'm just going to use a new name here so that we just get a unique script for each exercise that we're
working on.
So again in the real world you can keep the same name if you'd like.
Now really all the other requirements are still in play like you want to make sure that the script is
run with super user privileges but the only difference there is that when you display some help or an
error message when they don't then what you want to do is send that output to standard error instead
of standard output like we've been doing.
And that's going to be the same thing for all the error statuses that we're going to display in our
scripts so that's really the main thing that we're going to be changing here.
So here I am on my local system with my terminal open and I'm going to navigate to the class folder
from where I'm going to navigate to the vagrant project that we're using for this set of scripts and
that's local users.
And then I'm going to bring this virtual machine up and then connect to it.
Now I'm going to move into the shared folder of forward slash vagrant and then here I'm actually going
to copy the previous script we used and just copy it to this new name that we're going to work on today.
Of course you can write this entire script from scratch again if you would like just to give yourself
some extra practice.
But here for the walk through I'm just going to show you a couple of changes that you need to make from
the previous script in order to conform to these new requirements that we've come up with here.
So what I'm going to do is just start with the very top of the script and work my way down looking for
any changes that I need to make.
So here I see an exit status of one and an echo statement previously before.
So that's really our error message when we're exiting with a 1.
So what we need to do is send this output from the echo command to standard air right now by default
it's going to be displayed to standard output.
And so we can do this with a greater than symbol and ampersand and 2.
Now realize here that the greater than symbol assumes standard output.
So it's also the same as this if we want to be explicit and specify how far the descriptor one for standard
output.
I'm not going to do that.
I'm just going to delete that and then use this syntax The greater than ampersand too.
And of course as you know in ampersand right after a redirection operator implies that you're using
a file descriptor instead of a file name.
So again file descriptor 2 is associated with standard air.
So that makes the standard output here the Echo would generate be displayed on standard air.
Going to keep working my way down here.
Here I come to another exit statement here and I'm just going to do the same thing.
We're just going to send all this output to standard error
window redraw the screen with the cursor at the top and then just keep on working down here.
So assigning variables is not going to cause any output.
The shift command won't either the output of this command is stored in a variable so it's not getting
displayed anywhere.
OK this is a command that could possibly generate output.
Now let me go down the script a little bit here you can see that we're actually checking for its exit
status down here.
So we don't necessarily need to see the output generated by this command.
So actually what I'm going to do here is ignore all the output.
I don't need to see the standard output or the standard error from this command.
So to redirect both standard output and standard error will use ampersand greater than sign and then
we're going to send that to the dev null device and that's just going to throw away anything and it
gets so all the output generated by this command will not be displayed to the user running the script.
Again we come to another exit statement and that means this echo command right above it is going to
be an error message so we're going to send this to standard air.
OK so this password command we know generates some output.
So what we want to do is you know send that output to dev null so the user doesn't see this output.
So again we're going to use ampersand greater than and send that to the dev null device.
We're doing a check for an exit status of the password command and then we're going to display an error
if we get an error.
So that means this is an error message and needs to be displayed on standard air.
Now we have a another password command again.
The helpdesk users don't want to see these messages so we're just going to send all that output to dev
null.
Since we haven't generated any output in our script here there's no need for a blank line to separate
the information that we're going to display for the user between the output of the previous commands.
So I'm just going to get rid of this echo statement and then the rest of our script here generates the
output that we want the user to see.
So let's go ahead and Saveur changes and then we'll check the permissions on the script.
Now when you copy a script it gets the same permissions as the script it was copied from but we'll go
ahead and check the permissions here just to be thorough.
Hey it's 755.
So that looks good.
You can see the x's there.
And all three sets of permissions so anyone can execute the script.
Now obviously if they don't run it to privileges they're going to get an error message.
But again the executable bit is there.
If you created this script from scratch then you're going to need a chmod 755 your script.
So now that we have our changes in place or a new script written let's go ahead and test it.
So we're going to let's add a new user here and we'll just call this username Turing and the comment
will be Alan Turing and hit enter So now you can see that the only thing is displayed is the username
the password and the host that this user was created on.
None of that extra output from the password commands were displayed on the screen.
And this is exactly what we're looking for.
So going to go ahead and create our other users here we have was
again the display is just the information that is needed by the people running the command will create
another user here.
Call this more for Gordon Moore.
Kate we've created three accounts.
Now let's go ahead and see if those accounts were added to the ETSI password file.
We can do that by just telling the ANSI password file are actually going to do a dash 3 for the last
three lines of the ETSI password file.
OK.
There you can see our users.
Turing was and more.
Now I'm just going to switch to an account to be thorough here to make sure that it still forces the
password expiration and prompts us for a new password on log in.
As a matter of fact before I do this I need to go back up here and actually copy the password
and asking for a current password and then a new password
OK that looks like it worked.
And we're all done with this user so we'll just hit exit here and return to our normal vagrant user.
So the core functionality of our script is still in place it creates users that expire passwords and
so on.
So what we want to do is actually test our new functionality.
So one way to do that is to make our script generate an air and we can do that by not running it with
super user privileges.
Hey we get a message on our screen.
Of course you can't tell if that message is standard input or standard output but we'll get to that
in just a second.
It prints a message to our screen.
Now let's check the exit status of the command.
Sure enough it's an exit status of one.
Which is exactly what we want.
Now let's do the same thing again but this time let's redirect the standard output and standard error
to different places and then check those to see what is going where.
Now because by default standard output and standard error are both displayed to our screen we can't
necessarily tell what standard output or standard error just by looking.
So if we're going to do is redirect standard output to a file called STV out we can name that anything.
I'm just going to use the kind of standard abbreviation here.
Again standard error is going to go to a file.
I'm just going to name SDD.
E r r again you can name it any file name you want.
So all the output either goes to standard out or standard error.
So let's examine the standard elf file.
And there's nothing in that file.
And if we look at standard air then our error messages in that file.
So we know that the echo statement is displaying its output to standard error and that's exactly what
we want.
So let's go ahead and remove these files.
Let's test for another error condition that our script checks for.
We'll actually run the script here but we won't supply any arguments so will hit enter.
And sure enough we get an exit status here of one.
And now let's run the same kind of test.
We'll just send the standard output to standard out and the standard air to a standard error file.
We'll look to see if it generated in the standard output.
It did not.
So all the error messages went to standard error and that's exactly what we want our script to do.
We'll go ahead and clean up here.
All right.
Now that brings us to the end of this exercise so we've made our changes our error messages are now
being displayed to standard air instead of standard out and all that output that was generated from
those other commands are now hidden from the user.
Since I'm done for the day I'm going to exit out of the virtual machine and then go ahead and halt it.