We've already seen how we can use if statements, but
there's also a very simple way to test logic.
Let's gain some administrative privileges.
If we go through and search for the vagrant user, we can do that with the grep
command. Grep ‑q doesn't print anything, but if we don't print anything, how do
we know whether the vagrant user was there or not?
Well, we can query the success or failure of the last command
by looking in the special variable, $?.
If it's 0, it succeeded.
If it's an integer other than 0, then the command failed.
We can put that into use by taking a look at searching, let's say,
for the fred account. We don't need to display any information,
but if the fred account isn't there,
the double vertical bar, then we can go through and run a useradd ‑m
fred. We create the user, but only if the user does not exist, whereas in
the next example we can check that fred does exist, and using the &&, we
only set the password for the fred user if the fred user does actually
exist in our passwd file. We can implement this when we're looking at
creating our users, but we also need to work on our password generation
script.
We can go through and take a look to see if we have
received any input parameters,
arguments passed at the command line, and we're looking at the argument count.
If the argument count does not equal 1, so we've passed one argument through
to it, then we're going to be prompting to enter the password; else, if we
have set the argument 1$1, then we are going to set our reply variable to be
the $1. So this way, we only need to prompt for a password if we haven't
supplied it as our first argument. This is what we're going to look at first
in our demonstration. We're going to dive out to improve our password
generator.
We'll exit the script if the password is too short, and we're only going
to prompt for the password if it's not been supplied from the CLI. That
command line environment, nice and calming.
But remember, it's only calming if we are in the right branch of our
repository. Git status will then show me that I'm still in my
shellscripting, so I can continue to develop our scripts.
We want to be taking a look at developing the currently existing
password generation script so they can take command line arguments, and
so when we execute it we get better feedback.
If I put in a short password,
I have to know what type of password to put in, a longer
password, because it doesn't tell me.
So I put in a short password, then a long password, and it's going
to work, but that's because I know what to do.
Many users might not know what to do to generate that, so it's not that helpful.
We can make our script a little bit more helpful by extending the if
statement we've already implemented. We're checking our if statement
for the length of the password, but we only ran an if statement. We
can extend it with elseif or else. Else is then saying, if it is not
greater than 6 characters, then we're going to say that password is
too short.
We can specify that we must have at least 6 characters,
or more than 6 characters in our test.
And with that done, we could leave it like that and it would
allow us to loop around, but we also want to demonstrate the exit
status and the exit code and how bash knows whether or not a
command has succeeded or failed.
If we put, let's say, exit 2, then that's not 0. Zero is
success; anything other than 0 is a failure.
We can then go through and save our change. With that save made, we can go
through and implement our script now, and we're going to be able to see
that if I run it and put in a short password,
I get the error message coming back, I leave the script so we're not continuing
to loop around in our until loop. But also, if I investigate that special
variable, so echo out $?, we can query the exit status of the previous command
and we can see then that it relates to my number 2, the exit 2 that we put in,
so we know that this command failed.
We could research what the error code 2 meant so we could then
pass more information back to our users.
But as well as implementing this,
we want this to be more flexible so our users can supply command line
arguments if they don't want to be prompted for a password.
I'm moving to the top of their script, and if we move into the until loop,
whether you're using a while loop or until loop it's down to
you, but inside of that looping structure,
we're going to put a new if statement. With this if statement,
we're going to use the double square brackets.
The double square brackets makes it very convenient to be able to go through
and use variables without quoting them, so those variables can contain special
characters, but they won't cause any problems.
I'm going to implement a test on the variable $#. This is the number
of arguments that have been supplied to the script.
It doesn't include $0; $0 is a script name.
We're going to specify that it has to be one argument.
So we're saying if it's not equal to one argument,
so we don't have a $1, then we're going to go into our block of code. So
we want one, and only one argument being supplied.
If we haven't supplied the argument,
we will prompt for our password, so the same information that we had before,
but that's now contained in this test.
If we have supplied the argument,
if we have got the one and only argument that we are working with,
$1, then we can populate our REPLY variable with $1. REPLY variable
is a variable that we are using from our data that we're getting
back from the read command.
This way we only have to worry about the one REPLY variable
rather than two variables in our code.
We can close out our if statement with the fi command,
save our changes, and now we're ready to test.
So if I go through and provide a password from the command line,
straightaway then then I'm getting the password hash.
If I provide a short password, even from the command line,
I'm getting my exit message.
If I don't provide a password, then we can see that we're
taken into the prompting mechanism.
So, so long as I provide an adequately long password, we're able then to print out the password hash.