1 00:00:06,750 --> 00:00:09,140 - So let's start with this fellow, ldapusers. 2 00:00:10,950 --> 00:00:15,950 In which we have cn=lisa,dc=example,dc=con. 3 00:00:19,290 --> 00:00:21,480 And let's create three users. 4 00:00:21,480 --> 00:00:25,800 That's enough because if it works for three users, 5 00:00:25,800 --> 00:00:29,373 then it will also work for 300 users. 6 00:00:40,260 --> 00:00:41,400 So what do we need to do? 7 00:00:41,400 --> 00:00:45,870 Well, we need to process this file in the script. 8 00:00:45,870 --> 00:00:48,250 So let me create lab1.sh. 9 00:00:54,060 --> 00:00:56,580 Of course we start with the shebang. 10 00:00:56,580 --> 00:00:58,680 And I'm going to build it step by step. 11 00:00:58,680 --> 00:01:02,100 So let's start with for i in, in what? 12 00:01:02,100 --> 00:01:05,760 Well, in cat \ldapusers. 13 00:01:05,760 --> 00:01:07,260 Now the magic about this is that 14 00:01:07,260 --> 00:01:10,323 this is going to print every single line one by one. 15 00:01:12,601 --> 00:01:15,030 So, for i then we need do 16 00:01:15,030 --> 00:01:18,903 and then echo $i, done. 17 00:01:24,480 --> 00:01:26,370 Of course, this is not the entire solution, 18 00:01:26,370 --> 00:01:30,420 but I just want to verify that this little script 19 00:01:30,420 --> 00:01:33,360 allows us to use a for loop to print 20 00:01:33,360 --> 00:01:35,283 every single line from ldapusers. 21 00:01:40,110 --> 00:01:41,883 So we need to make it executable. 22 00:01:43,800 --> 00:01:45,033 And then we can run it. 23 00:01:46,140 --> 00:01:46,973 And there we go. 24 00:01:46,973 --> 00:01:48,360 This is looking good. 25 00:01:48,360 --> 00:01:49,800 Now next, we need to make sure 26 00:01:49,800 --> 00:01:52,290 that the username is extracted. 27 00:01:52,290 --> 00:01:54,120 So we need to remove cn equals, 28 00:01:54,120 --> 00:01:58,140 and we need to remove dc as example and dc as con. 29 00:01:58,140 --> 00:01:59,700 Now how are we going to do that? 30 00:01:59,700 --> 00:02:02,640 Well, I would say pattern matching operator. 31 00:02:02,640 --> 00:02:06,750 So, I am going to define a new variable. 32 00:02:06,750 --> 00:02:08,093 USER=. 33 00:02:08,970 --> 00:02:13,800 And then we are using $ and pattern matching operator on $i. 34 00:02:13,800 --> 00:02:15,630 So, what is the pattern matching operator 35 00:02:15,630 --> 00:02:16,980 that I want to use? 36 00:02:16,980 --> 00:02:21,570 Well, I want to remove the part from the beginning. 37 00:02:21,570 --> 00:02:25,230 So part from the beginning, that's a hash. 38 00:02:25,230 --> 00:02:26,853 Which part? *=. 39 00:02:29,670 --> 00:02:31,067 So USER=$i#*= 40 00:02:34,860 --> 00:02:38,223 that means that cn= should be removed. 41 00:02:40,620 --> 00:02:41,880 Run it again. 42 00:02:41,880 --> 00:02:46,620 User=$, and this time we run it on USER. 43 00:02:46,620 --> 00:02:50,220 And we do a pattern matching operator again to 44 00:02:50,220 --> 00:02:51,840 remove the longest match of 45 00:02:51,840 --> 00:02:54,543 anything comma followed by anything. 46 00:02:56,010 --> 00:02:58,410 And then we can do an echo $USER, 47 00:02:58,410 --> 00:03:00,718 and we will almost be there. 48 00:03:00,718 --> 00:03:03,633 If I did not make any mistakes of course. 49 00:03:06,840 --> 00:03:09,060 Uh oh, I did make a mistake. 50 00:03:09,060 --> 00:03:11,460 So, this mistake is unexpected. 51 00:03:11,460 --> 00:03:14,880 EOF while looking for the matching parenthesis. 52 00:03:14,880 --> 00:03:18,813 It looks as if I mixed up parenthesis and curly braces. 53 00:03:19,890 --> 00:03:21,810 That wouldn't be the first time that I'm doing that. 54 00:03:21,810 --> 00:03:22,860 There we go, $(USER). 55 00:03:24,570 --> 00:03:26,190 That's an easy to find error, 56 00:03:26,190 --> 00:03:28,650 that should be a curly brace. 57 00:03:28,650 --> 00:03:29,910 Let's try it again. 58 00:03:29,910 --> 00:03:31,920 And now we can see Lisa, Anna, and Bill. 59 00:03:31,920 --> 00:03:33,390 So, that is doing alright, 60 00:03:33,390 --> 00:03:34,980 so we can wrap it up. 61 00:03:34,980 --> 00:03:36,780 And how are we going to wrap it up? 62 00:03:36,780 --> 00:03:40,317 Well, by using echo useradd USER, $USER that is. 63 00:03:44,430 --> 00:03:45,660 And you know, in this case, 64 00:03:45,660 --> 00:03:47,250 I can even run it directly. 65 00:03:47,250 --> 00:03:48,990 We've already done all the testing. 66 00:03:48,990 --> 00:03:50,790 We need to create these users. 67 00:03:50,790 --> 00:03:53,195 So this will create the users. 68 00:03:53,195 --> 00:03:55,440 Lab1.sh, there we go. 69 00:03:55,440 --> 00:04:00,440 And cat on etc/passwd is showing that 70 00:04:00,840 --> 00:04:04,890 Anna and Lisa and Anna and Bill have been created. 71 00:04:04,890 --> 00:04:06,573 So mission accomplished.