- So let's start with this fellow, ldapusers.
In which we have cn=lisa,dc=example,dc=con.
And let's create three users.
That's enough because if it works for three users,
then it will also work for 300 users.
So what do we need to do?
Well, we need to process this file in the script.
So let me create lab1.sh.
Of course we start with the shebang.
And I'm going to build it step by step.
So let's start with for i in, in what?
Well, in cat \ldapusers.
Now the magic about this is that
this is going to print every single line one by one.
So, for i then we need do
and then echo $i, done.
Of course, this is not the entire solution,
but I just want to verify that this little script
allows us to use a for loop to print
every single line from ldapusers.
So we need to make it executable.
And then we can run it.
And there we go.
This is looking good.
Now next, we need to make sure
that the username is extracted.
So we need to remove cn equals,
and we need to remove dc as example and dc as con.
Now how are we going to do that?
Well, I would say pattern matching operator.
So, I am going to define a new variable.
USER=.
And then we are using $ and pattern matching operator on $i.
So, what is the pattern matching operator
that I want to use?
Well, I want to remove the part from the beginning.
So part from the beginning, that's a hash.
Which part? *=.
So USER=$i#*=
that means that cn= should be removed.
Run it again.
User=$, and this time we run it on USER.
And we do a pattern matching operator again to
remove the longest match of
anything comma followed by anything.
And then we can do an echo $USER,
and we will almost be there.
If I did not make any mistakes of course.
Uh oh, I did make a mistake.
So, this mistake is unexpected.
EOF while looking for the matching parenthesis.
It looks as if I mixed up parenthesis and curly braces.
That wouldn't be the first time that I'm doing that.
There we go, $(USER).
That's an easy to find error,
that should be a curly brace.
Let's try it again.
And now we can see Lisa, Anna, and Bill.
So, that is doing alright,
so we can wrap it up.
And how are we going to wrap it up?
Well, by using echo useradd USER, $USER that is.
And you know, in this case,
I can even run it directly.
We've already done all the testing.
We need to create these users.
So this will create the users.
Lab1.sh, there we go.
And cat on etc/passwd is showing that
Anna and Lisa and Anna and Bill have been created.
So mission accomplished.