Now we've already seen within our info script that we're able
to use for loops to iterate through a range.
Whatever we can do though in a script we can do at the command line.
Here we're just looking at a slightly different example
where we've got a static list of bill, bob,
and jayne,
and we can iterate through that list to run the
useradd command to create those users.
But as well as for loops, we have two different types of loops, while and until.
They're very similar.
While will keep on looping whilst the condition is true,
until will loop until the condition becomes true.
This is what we're going to look at now generating a password.
We'll see more information on this script when we get into the demonstration,
but having it here gives you a hard copy of what the script looks like.
So go on,
let's move out to our command line where we're going to look at
creating this password generation script.
In the script we're going to make use of variables,
our while or until loop, we can read user input,
we can test the length of entered data to make sure then
that we've got a password long enough,
and we can use if statements to end our looping structure.
So we're adding quite a lot into scripts now,
and hopefully you're going to be able to use this to
generate scripts for yourself.
Come on, let's find that command line.
And here we are back on our glorious command line,
a command line where we can check what we're aiming to create
in the final password generation script because we can look at
our original downloaded project.
If I move into comptia‑automation and shellscripting,
and the script we're going to create is pwgen.sh.
We see we're being prompted for a password.
If I put a short password in,
we're going to get then an exit and a warning message
that our password is too short.
If I put, though, a longer password, we get the password hash coming back,
and we could use this within our useradd command,
and we're going to have then a uniquely generated password each time.
But we want to move into our project in the
shellscripting directory where we have, at the moment,
just the single info.sh, and we're going to add to it then the pwgen.sh file.
And we know once we've created this,
the very first thing that we're going to do is put in our
shebang line to identify the type of file it is and the
interpreter that we are using.
Next up,
we're going to put in a variable that we are using
to control our looping structure.
If I go through and put PWD_OK,
and we can begin it with an initial value of false.
Now this is going to control our looping structure,
and we could do this with a while loop.
A while loop is going to loop continuously whilst the condition is true.
We test for that condition using square brackets,
and we can then go through and take a look at our
PWD_OK variable being not equal to true.
Now this is generally the way that I would do it,
but if you don't like the idea of the negative test in there,
not equal, well, we could change it to equals true,
but our looping condition then would be until,
until PWD_OK becomes true.
Either way is going to work, it's just what sits more comfortably with you.
Using the same do keyword as in a for loop,
we specify the code to run whilst we're in the loop,
and we're going to begin with the built‑in command read.
Read then prompts for user input,
‑s then silence echoing of our input to the screen because we
are prompting for a user to enter a password,
so we don't want that echoed back, ‑p is the prompt message.
I'm going to echo a clear and empty line after that so
we are clear of our prompt message.
We want to check that we've got the correct length of password,
so I'm going to do a PWD_LEN=.
And for this, again,
we're going to go into that structure where we look at the output of a command.
The variable that's being populated by read by default is called $REPLY.
We can echo it with a ‑n, so we suppress the line return.
So we're echoing out $REPLY,
and to count the number of characters so we've got the right length,
we can pipe it to word count ‑m to count the number of characters.
The ‑n becomes important so we don't count the new
line at the end of a standard echo.
We can go ahead and set our PASSWORD variable.
So this is going to contain the output of the password generation,
and we're going to use openssl for this.
And we can go and use the passwd module,
‑6, then go through and specify that we're using SHA‑512,
and then we want to input our $REPLY, our entered password.
We can begin with a very simple conditional statement using if.
And again, we use that same test condition as within our while loop,
and we can go through now and check for the length of our password,
so PWD_LEN.
We're going to check to see if this is greater than.
With numerical checks we use ‑gt for greater than or ‑lt for less than.
So we're looking for a password with more than 6 characters,
we use then instead of do to specify our code block.
And we can then go through and specify our PWD_OK to be now equals to true,
so we don't need to loop around again.
And we can echo out, just so the user knows what the encrypted password is,
we can echo out our $PASSWORD variable so we know then the password hash.
Ending an if statement is with the fi command,
so if backwards, and then we can put our done to end our do block.
Now once we are ready with that, we can go through and save our changes.
We can test it by adding in our execute permission and then
going through and executing the script.
We can see we've got a problem on line 7,
it looks like we're missing a parentheses,
so let's go through and check this.
I'd say it's worthwhile leaving in these mistakes to
show it's easy to make mistakes.
I can use uppercase A to move to the end of the line
and add in the closing parentheses, that should be okay now.
When I go through and enter it, again putting a short password,
I haven't put anything to exit the script,
so whilst we've got a short password, we keep looping around.
I can use Ctrl and C to end this.
If I want to go through and execute again,
I can put in a long password, and we'll see we won't have any problems,
so we now get our password hash being echoed out.
For the moment we are happy enough with the script,
so we can go through and use our git and add.
Remember it hasn't been added to the project yet so we have
to do it in the more longhand method.
And then we can go through and run a very simple git commit.
With our current changes saved, we're ready to edit our script just a little bit further.