1
00:00:02,990 --> 00:00:09,010
In this lesson you're going to be introduced to positional parameters and the for loop here on my local

2
00:00:09,010 --> 00:00:13,590
system I have a terminal open and I'm going to move into our class working folder.

3
00:00:15,300 --> 00:00:22,250
And from here into our vagrant project folder this project being local users and then now I'm going

4
00:00:22,250 --> 00:00:32,550
to start the virtual machine and then connect to it.

5
00:00:32,700 --> 00:00:40,080
Now we'll move into the shared folder of CD4 slash vagrant and this script I'm going to name all user

6
00:00:40,260 --> 00:00:42,520
Dymo 0 6 dot sh.

7
00:00:43,210 --> 00:00:49,470
Of course all scripts we start with the shebang and so will give the path to the bash interpreter.

8
00:00:49,690 --> 00:00:54,640
And then I'm just going to list the goal of this script or what this script does at the top here as

9
00:00:54,640 --> 00:00:55,270
our Hetter

10
00:01:05,530 --> 00:01:12,550
and the first thing we're going to do here is simply display what the user typed on the command line

11
00:01:13,320 --> 00:01:15,960
and we can do that with the echo command here.

12
00:01:24,120 --> 00:01:31,080
The variable of dollar zero is actually a positional parameter and positional parameters are variables

13
00:01:31,080 --> 00:01:34,160
that contain the contents of the command line.

14
00:01:34,230 --> 00:01:39,610
Before we go any further let's talk about the definition of a parameter versus an argument.

15
00:01:39,870 --> 00:01:44,870
A parameter is a variable that is being used inside the shell script.

16
00:01:45,060 --> 00:01:48,660
An argument is the data passed into the shell script.

17
00:01:48,660 --> 00:01:54,570
So an argument is applied on the command line becomes the value stored in a parameter.

18
00:01:54,570 --> 00:02:00,180
With that in mind the very first positional parameter is $ zero which contains the name of the script

19
00:02:00,180 --> 00:02:01,100
itself.

20
00:02:01,410 --> 00:02:06,550
You can think of this as being what the person typed in the command line to execute the script.

21
00:02:06,810 --> 00:02:12,150
The next positional parameter is dollar sign one which stores the value of the first argument passed

22
00:02:12,150 --> 00:02:13,970
to the script on the command line.

23
00:02:14,100 --> 00:02:19,350
The positional parameter dollar sign to source the second argument dollar sign three stores a third

24
00:02:19,350 --> 00:02:21,010
argument and so on.

25
00:02:21,240 --> 00:02:24,740
So now let's look at the output of our script so far.

26
00:02:26,480 --> 00:02:31,310
Of course we have to add executable permissions before we can execute our script so we'll do chmod

27
00:02:34,710 --> 00:02:36,310
and then execute the script.

28
00:02:37,670 --> 00:02:42,630
So notice how dollar signs 0 expanded to how we executed the script.

29
00:02:42,650 --> 00:02:45,560
This dot board slash in the script name.

30
00:02:45,560 --> 00:02:51,650
Now let's change how we call the script by providing the full path to the script so we can type forward

31
00:02:51,650 --> 00:02:59,970
slash vagrant user Dymo 06 and hit enter So again when the script is called in an explicit way like

32
00:02:59,970 --> 00:03:07,260
this then that is what is stored in dollar signed zero so dollar signs zero appears exactly as we have

33
00:03:07,260 --> 00:03:09,000
typed on the command line here.

34
00:03:09,790 --> 00:03:12,310
We're about to take a slight detour for a moment.

35
00:03:12,310 --> 00:03:16,270
Bear with me here because this will all tie into positional parameters.

36
00:03:16,360 --> 00:03:18,950
But again it's going to take several minutes to explain this.

37
00:03:18,950 --> 00:03:22,090
So again stick with me and be patient with me.

38
00:03:22,090 --> 00:03:27,730
So one thing I've glossed over is the command search path which is stored in the environment variable

39
00:03:27,730 --> 00:03:32,230
path that's ph th in all uppercase letters.

40
00:03:32,470 --> 00:03:38,170
Up until this point we've just been supplying either a fool or relative path to the scripts we've created.

41
00:03:38,170 --> 00:03:42,340
Let's take a quick look at what the bash man page has to say about the PATH variable.

42
00:03:42,370 --> 00:03:52,200
Before we move forward we'll do a forward search with a forward slash followed by Path.

43
00:03:52,380 --> 00:03:55,410
We'll just keep hitting in until we get to the PATH variable.

44
00:03:55,470 --> 00:03:57,070
And here it is.

45
00:03:57,140 --> 00:04:04,370
It says that path is a colon separated list of directories in which the shell looks for commands.

46
00:04:04,370 --> 00:04:09,080
Now if you were to look at the command execution section that it references here on your screen you'll

47
00:04:09,080 --> 00:04:12,310
find that every time you type something in it the command line.

48
00:04:12,410 --> 00:04:16,400
Bash first tries to find a function with that name and execute it.

49
00:04:16,400 --> 00:04:19,330
Now we haven't covered functions yet but don't worry we will.

50
00:04:19,520 --> 00:04:24,830
So if it doesn't find a function with that name it looks for that command and it's a list of built in

51
00:04:24,830 --> 00:04:25,760
commands.

52
00:04:25,880 --> 00:04:29,090
If it is a shell builtin command then it executes that.

53
00:04:29,090 --> 00:04:34,350
Now you've been working with several built in commands such as Echo read type help and so on.

54
00:04:34,640 --> 00:04:39,560
Now if the command you type at the command prompt or on the command line is not a function or it's not

55
00:04:39,560 --> 00:04:45,350
a built in then bash searches through the list of directories defined in the PATH variable and executes

56
00:04:45,350 --> 00:04:52,490
the first match that it finds if no match is found then a command not found error message is displayed.

57
00:04:52,880 --> 00:04:55,500
So let's get on the main page here by typing queue.

58
00:04:55,730 --> 00:05:00,410
And let's look at our path Ecko dollar path.

59
00:05:01,170 --> 00:05:07,020
So in our particular case here if a command is not a function or a shell built in then bash will look

60
00:05:07,020 --> 00:05:13,980
for that command first and user local bin then end user bin then it will look and user local Esben and

61
00:05:13,980 --> 00:05:15,420
so on down the list.

62
00:05:16,550 --> 00:05:19,790
It's important to point out that the path can be changed.

63
00:05:19,790 --> 00:05:24,450
So it's not something that is guaranteed to be the same for every user across every system.

64
00:05:24,590 --> 00:05:30,200
So keep that in mind by the way if you want to control the path you can do so in your script by manipulating

65
00:05:30,200 --> 00:05:31,730
the PATH variable there.

66
00:05:32,000 --> 00:05:36,520
Let's use a neat little command called which to tell us which command will be executed.

67
00:05:36,650 --> 00:05:42,230
Let's take the head command that we've used before as an example which Ed.

68
00:05:42,300 --> 00:05:48,120
So this tells us that when we type and head at the command line the program located at user bin head

69
00:05:48,120 --> 00:05:54,180
will be executed the first directory in our path variable is actually user local Ben.

70
00:05:54,300 --> 00:06:00,240
So let's create our own shellscript named head and place it in there we'll need root privileges to do

71
00:06:00,240 --> 00:06:00,730
that.

72
00:06:00,870 --> 00:06:08,070
So I'm going to create the file with Sue them user local been had and then here we're just going to

73
00:06:08,070 --> 00:06:09,680
create a little shell.

74
00:06:10,840 --> 00:06:15,970
Hello from my head and then we'll go ahead and save it.

75
00:06:16,670 --> 00:06:19,200
We'll add the executable bit to it here.

76
00:06:23,490 --> 00:06:28,160
Now let's see which head will be executed so I'll type in which head.

77
00:06:28,290 --> 00:06:31,580
And it says user local bin head will be executed.

78
00:06:31,830 --> 00:06:36,960
Let's use the dash option to which which forces it to print all matching executables that are in our

79
00:06:36,960 --> 00:06:37,580
path.

80
00:06:39,100 --> 00:06:42,400
So there are the two commands named the exact same thing.

81
00:06:42,400 --> 00:06:43,400
Both are named head.

82
00:06:43,480 --> 00:06:46,120
But the first one that is found is executed.

83
00:06:46,120 --> 00:06:50,940
So let's go ahead and just execute this command type and head and hit enter.

84
00:06:50,950 --> 00:06:53,860
Sure enough our version of the head command is executed.

85
00:06:53,860 --> 00:06:59,170
Now if you want to override the default then just supply the full path so we can do that with user been

86
00:06:59,180 --> 00:07:05,260
had let's say if we would just want the first line of the ETSI password file so execute that.

87
00:07:05,260 --> 00:07:10,290
So this is what we've been doing all along which is explicitly defining a path to a command.

88
00:07:10,360 --> 00:07:11,910
Actually to our scripts.

89
00:07:12,160 --> 00:07:18,820
Let's get rid of the version of our head so we'll just you say to our M User local then head here is

90
00:07:18,910 --> 00:07:24,210
one little quirk with rapidly adding and removing programs from your path.

91
00:07:24,400 --> 00:07:29,440
BASH uses a hash table to remember the full path names of executable files.

92
00:07:29,500 --> 00:07:33,080
It looks up the location to the executable once and then remembers it.

93
00:07:33,280 --> 00:07:39,820
If you look at the type head command which will do here type read it says that head is cached to user

94
00:07:39,850 --> 00:07:41,270
local bin head.

95
00:07:41,350 --> 00:07:47,520
That simply means that bash will execute that command at the path without first searching for that command.

96
00:07:47,740 --> 00:07:49,160
So here's the quirk.

97
00:07:49,330 --> 00:07:55,300
BASH thinks that head is still located at user local been had held for we just deleted it just a few

98
00:07:55,300 --> 00:07:56,170
seconds ago.

99
00:07:56,350 --> 00:07:59,050
So let's execute the command and see what happens.

100
00:07:59,050 --> 00:08:03,930
So if we type in head and hit enter and says user local bin had no such file or directory.

101
00:08:04,090 --> 00:08:09,760
So if you run into this type of issue you can make bash forget all these hashed locations by using the

102
00:08:09,760 --> 00:08:12,760
hash built in with hash dash are.

103
00:08:12,970 --> 00:08:18,430
Now if we do type head we can see that it searches the path and it finds the one that really is there

104
00:08:18,600 --> 00:08:20,330
and user bin.

105
00:08:20,380 --> 00:08:25,480
Of course you can read about the hash a shell built in with the command help hash and there you can

106
00:08:25,480 --> 00:08:30,880
see the dash r option that we use says to forget all remembered locations.

107
00:08:30,880 --> 00:08:36,600
Now typically hashing is not a problem because commands don't usually just randomly appear and disappear.

108
00:08:36,670 --> 00:08:41,550
During this same shell session and probably not during the execution of a shell script.

109
00:08:41,620 --> 00:08:44,080
In any case it's just something to be aware of.

110
00:08:44,840 --> 00:08:50,330
OK so I know that was a rather long aside but let's bring this back around to positional parameters

111
00:08:50,330 --> 00:08:53,130
and specifically about dollar signs zero.

112
00:08:53,360 --> 00:08:56,950
First let's prove that our script is not in our current path.

113
00:08:56,960 --> 00:08:58,360
We can do that with a which command.

114
00:08:58,490 --> 00:08:59,890
Which user.

115
00:08:59,910 --> 00:09:01,340
Demo 0 6.

116
00:09:01,520 --> 00:09:05,990
And sure enough there is no user demo 6 in our path and it lists our path there.

117
00:09:05,990 --> 00:09:08,480
Use your local bin user bin and so on.

118
00:09:08,480 --> 00:09:11,690
Now let's take a copy of our script and place it into our path.

119
00:09:11,690 --> 00:09:19,520
By putting it into user local bin and we can do that was soooo sue for copy and then provide the path

120
00:09:19,820 --> 00:09:24,890
to the directory want to copy it to and hit enter.

121
00:09:27,520 --> 00:09:35,040
Now our script is in our path so we can execute it without supplying a path to the script.

122
00:09:35,040 --> 00:09:38,220
Now notice what's stored in dollar sign 0.

123
00:09:38,280 --> 00:09:45,150
It's the full path to the command which is user local bin l user Dasch demo 06 dot sh.

124
00:09:45,450 --> 00:09:50,860
Even though we only typed in l user dasht him 06 not SH at the command line bash.

125
00:09:50,880 --> 00:09:54,290
Found it in our path and executed it at that path.

126
00:09:54,450 --> 00:09:58,530
And that's why the path and the file is stored in dollar signs zero.

127
00:09:59,010 --> 00:10:04,350
So let's review what gets stored in dollar signs 0 in different situations so we'll do that forward

128
00:10:04,350 --> 00:10:11,440
slash l user Dymo 6 so dot slashes what is executed and that's what's in dollar signs 0.

129
00:10:11,640 --> 00:10:17,610
If we're explicit again and provide a full path such as the Ford site a vagrant user Dymo 6 that will

130
00:10:17,610 --> 00:10:19,210
be reported as well.

131
00:10:19,230 --> 00:10:27,450
However if a command is our path and we execute it like so then it reports the full path to that command.

132
00:10:27,860 --> 00:10:32,600
Hey before we move on let's remove the script out of our path before we continue so will do Sea-Doo

133
00:10:32,640 --> 00:10:38,050
r.m user local BNL user demos 6. sh.

134
00:10:38,240 --> 00:10:44,960
There's a command called basename that removes the directory component and just returns the file name.

135
00:10:44,960 --> 00:10:51,110
So let's read up about that command man base name here at the top of your screen you can see that basename

136
00:10:51,350 --> 00:10:54,580
strips out the directory and suffix from filenames.

137
00:10:54,590 --> 00:10:56,920
So let's see what happens when we execute this.

138
00:10:56,920 --> 00:11:05,300
We'll just hit Q to get all the help page there and we'll type base name vagrant user demo 6. SH and

139
00:11:05,300 --> 00:11:11,180
then it just returns just the file name without the directory portion of the file.

140
00:11:11,210 --> 00:11:15,100
By the way basename isn't doing any smart checking to see if the file exists.

141
00:11:15,110 --> 00:11:20,420
It just strips off the directory portion of a string that is passed to it so we can give the base name

142
00:11:20,420 --> 00:11:22,640
something that clearly doesn't exist.

143
00:11:22,730 --> 00:11:24,590
But it will still do its job so we can do.

144
00:11:24,590 --> 00:11:28,600
Base name port slash not forward slash here.

145
00:11:28,850 --> 00:11:35,450
And of course it reports here because that's the file name portion of that path to that file even though

146
00:11:35,450 --> 00:11:39,350
the file doesn't exist and we can prove that with ls not here.

147
00:11:39,350 --> 00:11:43,010
Sure enough there's no such file or directory counterpart.

148
00:11:43,040 --> 00:11:48,960
The basename command is D-I our name so let's look at the man page for that.

149
00:11:49,000 --> 00:11:53,700
This says that Dhia are named strip's the last component from the file name.

150
00:11:53,910 --> 00:11:59,060
Let's get you to exit the main page and execute the command Symbol type Diyar name.

151
00:11:59,120 --> 00:12:06,340
They grant all user demo 6 and hit enter and forward slash vagrant is returned because that is the directory

152
00:12:06,340 --> 00:12:14,040
name or the directory portion of that full path like basename D-I our name doesn't do any smart checking.

153
00:12:14,080 --> 00:12:19,060
We can give it a non-existent path and it will simply return the portion that looks like the directory

154
00:12:19,480 --> 00:12:21,190
so we can do this idea or name.

155
00:12:21,190 --> 00:12:23,760
This is not here.

156
00:12:23,770 --> 00:12:24,820
Sure enough it returns.

157
00:12:24,820 --> 00:12:29,630
This is not because it looks like the here file is in the directory.

158
00:12:29,770 --> 00:12:32,880
Ford slash this forward slash is for Slash not.

159
00:12:33,040 --> 00:12:40,290
But of course that file and directory that whole tree doesn't exist but Deeya our name doesn't check

160
00:12:40,290 --> 00:12:41,220
it doesn't care.

161
00:12:41,340 --> 00:12:47,110
It just returns the path or the directory of the file again.

162
00:12:47,130 --> 00:12:52,740
Obviously this works on things that do exist but it doesn't matter to the Diyar are named command or

163
00:12:52,740 --> 00:12:54,450
base named command.

164
00:12:54,450 --> 00:12:57,740
Now let's use these two commands in our script.

165
00:13:01,800 --> 00:13:07,530
So what we're going to do is display the path and filename of the script

166
00:13:23,580 --> 00:13:29,340
in earlier lessons you use a dollar sign followed by a command that was wrapped in parentheses to assign

167
00:13:29,370 --> 00:13:35,040
the output of that command to a variable that is called command substitution.

168
00:13:35,040 --> 00:13:40,230
In this case we are only going to use the output of each of these commands once so we just included

169
00:13:40,230 --> 00:13:43,110
them right in the echo command.

170
00:13:43,110 --> 00:13:49,020
Again with this dollar sign parentheses syntax does or this command substitution syntax does is return

171
00:13:49,020 --> 00:13:51,150
the output of the command.

172
00:13:51,210 --> 00:13:56,550
You can assign the output to a variable or use that output without assigning it to a variable.

173
00:13:56,550 --> 00:13:58,350
Either way is fine.

174
00:13:58,350 --> 00:14:03,130
Perhaps assigning the output of the commands to a variable first makes for a bit cleaner code.

175
00:14:03,210 --> 00:14:08,280
But I just wanted to show you that you don't have to use command substitution and variable assignment

176
00:14:08,310 --> 00:14:08,850
only.

177
00:14:08,940 --> 00:14:11,460
You can use it in line as well.

178
00:14:11,550 --> 00:14:14,570
So let's save an execute our script and see what we get here.

179
00:14:18,430 --> 00:14:24,760
The first line says you executed this command which is dollar signs 0.14 Lachelle user dasht him 0 6

180
00:14:24,760 --> 00:14:25,870
that sh.

181
00:14:25,900 --> 00:14:30,460
You use dot as the path to the user Dasch demo 6. sh script.

182
00:14:30,700 --> 00:14:37,720
So Dot was the output of the Dhia our name command and l use your dash Dymo 6 that SH was the output

183
00:14:37,780 --> 00:14:39,930
of the basename command.

184
00:14:39,940 --> 00:14:46,070
So let's try executing this script in another way will provide the full path forward slash vagrant Elisir

185
00:14:46,180 --> 00:14:48,720
Dymo 6. Sh.

186
00:14:48,760 --> 00:14:54,310
So this time it lists Ford slash vagrant as the path and then of course the base name of the script

187
00:14:54,310 --> 00:14:57,580
is l user Dasht demo 06 dot sh.

188
00:14:57,980 --> 00:15:00,210
OK let's look at the bash man page real quick.

189
00:15:01,360 --> 00:15:08,070
And we're looking for the special parameter section of the page here and then we'll keep hitting and

190
00:15:08,070 --> 00:15:14,610
to continue to our next match and then the thing I want to point out here is the pound sign and the

191
00:15:14,610 --> 00:15:18,980
pound sign it says expands to the number of positional parameters in decimal.

192
00:15:19,140 --> 00:15:23,960
So a dollar sign pound sign tells us how many arguments were supplied on the command line.

193
00:15:23,970 --> 00:15:26,300
Let's continue with our script here.

194
00:15:26,580 --> 00:15:32,490
Menus being v to execute the most recent command that starts with the V which is a VM so I can get back

195
00:15:32,490 --> 00:15:34,260
into my editor really quickly here.

196
00:15:35,530 --> 00:16:01,610
So let's tell the user how many arguments they passed in.

197
00:16:01,700 --> 00:16:07,890
Remember that the curly braces are optional so some people might actually just use dollar sign hash

198
00:16:07,940 --> 00:16:08,530
sign.

199
00:16:08,600 --> 00:16:13,970
So just be aware that if you're reading other people's scripts but I'm willing to use the curly braces

200
00:16:13,970 --> 00:16:27,610
here to be very clear.

201
00:16:27,840 --> 00:16:33,510
Of course here you could have simply used the dollar sign hash sign directly in the echo command similar

202
00:16:33,510 --> 00:16:37,590
to what we did with the command substitution in the previous echo command.

203
00:16:37,590 --> 00:16:39,520
It's really a style choice.

204
00:16:39,570 --> 00:16:42,370
I just wanted to see how you can do it both ways.

205
00:16:42,530 --> 00:16:47,460
So the first time I didn't use a variable this time I used a variable and then used the value of that

206
00:16:47,460 --> 00:16:49,510
variable in the echo command.

207
00:16:50,590 --> 00:16:53,280
So let's save our changes and then do some testing here.

208
00:16:53,290 --> 00:17:00,000
So we'll just execute our script says you supplied zero arguments on the command line.

209
00:17:00,000 --> 00:17:06,630
So let's just type something in after the command line or just type hello and it says we supplied one

210
00:17:06,630 --> 00:17:09,690
argument on the command line and will type in another word.

211
00:17:09,720 --> 00:17:12,500
You supply two arguments and so on.

212
00:17:12,510 --> 00:17:18,870
So this one says you supplied three and that is giving us the proper information so it looks like Dollar

213
00:17:18,870 --> 00:17:21,650
Sign hash sign works and we're all good there.

214
00:17:21,720 --> 00:17:23,960
So let's continue editing our script.

215
00:17:25,740 --> 00:17:31,140
Because the real goal of this little example shell script that we're working on is to generate a random

216
00:17:31,140 --> 00:17:34,410
password for each user that's supplied on the command line.

217
00:17:34,590 --> 00:17:38,880
We want to make sure that at least one user is supplied on the command line.

218
00:17:38,880 --> 00:17:41,370
So let's do a check with an if statement.

219
00:17:48,550 --> 00:17:55,210
So use if space double opening brackets space when we use a variable and between brackets here we're

220
00:17:55,210 --> 00:18:03,680
going to enclose it in quotation marks and the variable we're going to look at is number of parameters.

221
00:18:03,910 --> 00:18:09,100
And then we want to make sure that that's Dasch L T which stands for less than one.

222
00:18:09,490 --> 00:18:17,290
So if the number of parameters is less than 1 then what we're going to do is echo a usage statement

223
00:18:17,300 --> 00:18:19,210
much like you would see in a man page

224
00:18:22,590 --> 00:18:25,180
instead of manually typing in the name of the script.

225
00:18:25,350 --> 00:18:30,270
We can just simply use dollar sign 0 to echo back more or less what the user typed in and the command

226
00:18:30,270 --> 00:18:33,100
line and then we'll tell them what they need to do.

227
00:18:33,180 --> 00:18:41,230
They need to supply a user name and optionally they can supply another user name and actually optimally

228
00:18:41,250 --> 00:18:45,670
they can supply even more usernames so we'll use the ellipsis there.

229
00:18:45,720 --> 00:18:50,880
Now if they get to this point that means that the script did not successfully execute because they didn't

230
00:18:50,880 --> 00:18:54,000
give us any people to create passwords for.

231
00:18:54,000 --> 00:18:59,660
So what we're going to do is exit with a non-zero exit status and I'm just going to use one here.

232
00:18:59,730 --> 00:19:02,090
Those are if statement with f I.

233
00:19:02,760 --> 00:19:04,830
Now by the way let's look at the first line here again.

234
00:19:04,830 --> 00:19:08,460
If the number of parameters is less than 1 then bubble block.

235
00:19:08,730 --> 00:19:13,740
So here what I'm actually doing is just demonstrating something that we haven't used before just a slight

236
00:19:13,740 --> 00:19:16,450
variation of Dash l t.

237
00:19:16,560 --> 00:19:23,250
And where did I get that from a hopefully remember that you can get help on these tests by looking at

238
00:19:23,280 --> 00:19:25,450
help test at the command line.

239
00:19:25,470 --> 00:19:31,290
Of course you'll find in there that Dasch LTE stands for less than of course what is the only thing

240
00:19:31,290 --> 00:19:37,230
that's less than one in our particular situation working with whole numbers here on the command line.

241
00:19:37,230 --> 00:19:42,490
Negative numbers don't make sense because you can't supply negative arguments and this sorts of thing.

242
00:19:42,510 --> 00:19:46,200
So obviously the the only thing that's less than 1 is zero.

243
00:19:46,200 --> 00:19:52,560
So you could have said if the number of parameters equals zero then give them the usage statement and

244
00:19:52,560 --> 00:19:53,600
exit.

245
00:19:53,610 --> 00:19:55,540
So again either way is right.

246
00:19:55,560 --> 00:19:59,020
It's just your personal preference how you think what makes sense to you.

247
00:19:59,040 --> 00:20:04,410
It's style again both ways work just whatever comes to mind just use that.

248
00:20:04,440 --> 00:20:09,550
Again I'm using this here so you can see some slight variations in different ways to do this.

249
00:20:09,570 --> 00:20:14,630
Now let's see if our changes and start passing in varying number of arguments to our script.

250
00:20:17,470 --> 00:20:18,220
We'll hit enter.

251
00:20:18,220 --> 00:20:25,000
We did not use any arguments on the command line and we get our usage statement and if we check the

252
00:20:25,000 --> 00:20:29,260
return code with dollar sign question mark or you can actually do this dollar sign question mark with

253
00:20:29,260 --> 00:20:33,680
the curly braces then sure enough we get an exit status of one from our script.

254
00:20:33,850 --> 00:20:36,370
Since it didn't successfully execute all the way.

255
00:20:36,670 --> 00:20:41,320
So let's get back to our script and let's just give it one thing sure enough you supplied one argument

256
00:20:41,980 --> 00:20:50,440
to let's do a b c d ok that's for arguments and I want to point out something here that we used actually

257
00:20:50,440 --> 00:20:58,000
with the user add command in an earlier script which is enclosing things in quotation marks so a b c

258
00:20:58,420 --> 00:20:59,160
d.

259
00:20:59,470 --> 00:21:05,230
So this says that you supplied three arguments on the command line but maybe at first glance you think

260
00:21:05,230 --> 00:21:12,620
that B and C are two different things but in reality because they're enclosed in quotation marks they're

261
00:21:12,640 --> 00:21:14,950
treated as one argument.

262
00:21:14,950 --> 00:21:17,130
Let's do this again let's see this one.

263
00:21:17,260 --> 00:21:20,050
This whole thing is two.

264
00:21:20,290 --> 00:21:22,750
And here is three.

265
00:21:22,780 --> 00:21:27,190
So again in this instance we supplied three arguments on the command line.

266
00:21:27,190 --> 00:21:32,000
Another thing you can do is simply enclose everything in quotation marks and that's fine as well.

267
00:21:33,790 --> 00:21:37,640
I'm just going to use singlehood marks because that works to Dujuan.

268
00:21:37,720 --> 00:21:42,420
This is to and this is theory.

269
00:21:42,550 --> 00:21:47,470
So again three arguments on the command line which interprets to three positional parameters inside

270
00:21:47,470 --> 00:21:48,430
the script.

271
00:21:48,430 --> 00:21:54,670
There are three because they are enclosed in quotes so it doesn't split at spaces when the data passed

272
00:21:54,670 --> 00:21:57,760
in is enclosed in quotation marks.

273
00:21:57,760 --> 00:22:02,950
Now again the idea behind this script is that we're going to perform the same action over a list of

274
00:22:02,950 --> 00:22:03,340
items.

275
00:22:03,340 --> 00:22:06,690
Now this is the perfect time to use a for loop.

276
00:22:06,850 --> 00:22:13,160
Let's take a second and look at the four shell built in the type dash.

277
00:22:13,160 --> 00:22:15,630
A command says for is a shell keyword.

278
00:22:15,800 --> 00:22:17,060
So that's a shell built in.

279
00:22:17,060 --> 00:22:19,020
And so it can do is get help for.

280
00:22:19,280 --> 00:22:23,150
I know there's going to be a good bit of information that pipes up to the screen so I'm just going to

281
00:22:23,150 --> 00:22:23,630
pipe this.

282
00:22:23,630 --> 00:22:28,360
The head command to get just the first 10 lines of output.

283
00:22:28,540 --> 00:22:33,940
Hey the first line of the help says for name and words do commands.

284
00:22:33,940 --> 00:22:41,260
Done the name you see in the help here is actually a variable name for each item in the list of words.

285
00:22:41,300 --> 00:22:44,060
The variable name is set to that item.

286
00:22:44,120 --> 00:22:49,470
Then the commands are executed and the process is repeated for each item in the list.

287
00:22:49,700 --> 00:22:56,730
Let's create a loop right here at the command prompt will do for x x is the name or our variable name.

288
00:22:56,840 --> 00:22:59,620
And and we're going to provide a list to do this.

289
00:22:59,620 --> 00:23:05,300
Frank Lear and Doug The next thing we want to do is type in do.

290
00:23:05,310 --> 00:23:12,290
Hit enter and then it's stylistically appropriate to indent so I'll just put a couple of spaces here.

291
00:23:12,350 --> 00:23:17,960
Echo type hi and then refer to the variable name here.

292
00:23:18,080 --> 00:23:23,650
X and yet inner and close are for loop with done.

293
00:23:24,840 --> 00:23:30,920
In this example x is first assigned to Frank then the command of Echo gets executed using that variable

294
00:23:30,920 --> 00:23:36,800
dollar sign X. Then the next time the loop repeats player is assigned to x.

295
00:23:36,830 --> 00:23:37,840
The commands execute.

296
00:23:37,850 --> 00:23:41,880
And finally Doug is assigned to x and the commands execute.

297
00:23:41,910 --> 00:23:48,470
If I hit the up arrow here to pull up the command you can see the semi-colons which represent the command

298
00:23:48,470 --> 00:23:49,460
separators.

299
00:23:49,640 --> 00:23:57,200
So if you want to you can place this command all on one line but it's easier to read if you do not do

300
00:23:57,200 --> 00:23:57,490
that.

301
00:23:57,500 --> 00:23:59,270
Again it's valid syntax here.

302
00:23:59,330 --> 00:24:02,390
Like you see at the Help for on your screen.

303
00:24:02,390 --> 00:24:08,330
But again it's just easier to read if you put these on multiple lines when I hit enter again to execute

304
00:24:08,330 --> 00:24:09,270
that again.

305
00:24:09,410 --> 00:24:14,750
If you look near the top of your screen in The Help output for the four key word it says if in words

306
00:24:14,750 --> 00:24:19,090
is not present then dollar sign as sign is assumed.

307
00:24:19,490 --> 00:24:24,130
So let's find out what dollar sign at sign is by looking at the bash man page.

308
00:24:25,890 --> 00:24:29,310
I'm going to look for the special parameters here and the main page.

309
00:24:30,640 --> 00:24:32,500
Here's the section.

310
00:24:32,500 --> 00:24:36,190
Now let's start with the at sign special parameter.

311
00:24:36,220 --> 00:24:42,340
It expands the positional parameters starting from 1 when the expansion occurs within double quotes.

312
00:24:42,340 --> 00:24:49,540
Each parameter expands to a separate word that is dollar sign is equivalent to dollar sign one dollar

313
00:24:49,540 --> 00:24:51,190
sign too and so on.

314
00:24:51,190 --> 00:24:55,120
Now this is nearly the same as Dollar Sign asterisk.

315
00:24:55,120 --> 00:24:58,420
It also expands to the positional parameters starting from 1.

316
00:24:58,420 --> 00:25:03,300
However if it's enclosed in double quotes it expands to a single word.

317
00:25:03,340 --> 00:25:08,320
We don't want to treat all the parameters as a single word what we want is a list of words as they were

318
00:25:08,330 --> 00:25:10,190
supplied on the command line.

319
00:25:10,210 --> 00:25:15,790
That's why we're going to use dollar sign at sign and not dollar sign asterisk in our script.

320
00:25:15,790 --> 00:25:20,430
By the way the use of dollar sign asterisk and dollar sign at sign are often confused.

321
00:25:20,770 --> 00:25:38,650
OK let's get out of the main page here with Q and get back to editing our script.

322
00:25:38,730 --> 00:25:44,310
So we're going to use a for loop will do for will supply a variable name we'll call these usernames

323
00:25:44,400 --> 00:25:48,770
user underscore name and use quotations here.

324
00:25:49,200 --> 00:25:52,130
Dollar Sign at sign do.

325
00:25:52,530 --> 00:25:56,820
So we're going to use the command we use to generate passwords in the previous script.

326
00:25:56,820 --> 00:26:02,610
So we'll do that with pass word equals well use command substitution with dollar sign followed by an

327
00:26:02,610 --> 00:26:03,600
opening parentheses

328
00:26:12,180 --> 00:26:26,750
and then we're going to display that user name and password.

329
00:26:26,760 --> 00:26:33,080
Now obviously we can't know exactly how many positional parameters a user will attempt to supply at

330
00:26:33,090 --> 00:26:34,070
the command line.

331
00:26:34,110 --> 00:26:37,010
We can check for them but we just don't know in advance.

332
00:26:37,020 --> 00:26:42,840
So instead of you know generating a password for dollar sign one dollar sign two dollar sign three and

333
00:26:42,840 --> 00:26:43,680
so on.

334
00:26:43,680 --> 00:26:50,700
Even though they may only supply two arguments for example then we're going to do is use dollar sign

335
00:26:50,700 --> 00:26:51,650
at sign.

336
00:26:51,870 --> 00:26:57,090
So no matter how many arguments they pass into the script it is covered by dollar sign and sign so we

337
00:26:57,090 --> 00:27:00,050
don't have to guess what the user is going to do.

338
00:27:00,450 --> 00:27:04,120
They can supply one to three or ten or more.

339
00:27:04,200 --> 00:27:06,400
And this script is going to work just fine.

340
00:27:11,330 --> 00:27:15,070
Well just supply one thing on the command line and see what happens.

341
00:27:15,070 --> 00:27:20,070
So sure enough the for loop executed one time generated a password and displayed it for the user.

342
00:27:20,260 --> 00:27:22,800
Let's add another user on the command line.

343
00:27:24,790 --> 00:27:26,360
Let's said some more actually.

344
00:27:26,370 --> 00:27:31,260
So by using the for loop in conjunction with dollar sign outside and we were able to account for everything

345
00:27:31,260 --> 00:27:37,050
passed in on the command line just for demonstration purposes I want to show you what the differences

346
00:27:37,350 --> 00:27:39,750
between dollar sign and sign and dollar sign.

347
00:27:39,750 --> 00:27:40,680
Asterisk.

348
00:27:40,680 --> 00:27:44,250
So let's just go back into our script and make that change.

349
00:27:48,220 --> 00:27:55,480
So here I'm going to change the at sign to an asterisk save our changes and then execute our script

350
00:27:55,480 --> 00:27:56,240
again.

351
00:27:56,500 --> 00:28:01,590
So as you can see it tweeted everything on the command line as one argument.

352
00:28:01,720 --> 00:28:07,090
So the for loop only ran one time and generated a password for that one argument.

353
00:28:07,090 --> 00:28:11,590
Again that's the difference between dollar sign asterisk and dollar sign at sign.

354
00:28:11,710 --> 00:28:17,860
Let's go back and fix our script here and change this asterisk to the at symbol here.

355
00:28:18,190 --> 00:28:20,670
Save our changes and check our script.

356
00:28:20,800 --> 00:28:23,630
And sure enough it runs like we want it to.

357
00:28:23,650 --> 00:28:28,800
Let's take just a minute to recap this lesson and by the way we covered a lot here.

358
00:28:28,930 --> 00:28:31,630
First you learned about positional parameters and how.

359
00:28:31,630 --> 00:28:35,080
Dollar Signs zero expansive the name of the shelf skip and dollar sign.

360
00:28:35,090 --> 00:28:40,420
One expands to the first argument supplied on the command line dollar sign to expands to the second

361
00:28:40,420 --> 00:28:42,090
argument and so on.

362
00:28:42,100 --> 00:28:47,320
You also learned a few different special positional parameters such as Dollar Sign pound sign which

363
00:28:47,320 --> 00:28:49,260
represents the number of parameters.

364
00:28:49,300 --> 00:28:54,310
Dollar Sign asterisk which expands to the list of positional parameters starting at dollar sign one

365
00:28:54,730 --> 00:28:59,880
and dollar sign at sign which also expands to the list of positional parameters starting a dollar sign

366
00:28:59,880 --> 00:29:00,750
one.

367
00:29:00,790 --> 00:29:06,730
However the difference between an asterisk and dollar sign and sign is only noticed when enclosing them

368
00:29:06,730 --> 00:29:08,440
in quotation marks.

369
00:29:08,440 --> 00:29:14,410
That difference is that dollar sign Asterix treats all the positional parameters as a one string and

370
00:29:14,410 --> 00:29:17,890
dollar sign and sign treats them as separate words.

371
00:29:17,890 --> 00:29:22,510
We also took a slight detour in the middle of the lesson to talk about how the path shell variable is

372
00:29:22,510 --> 00:29:28,450
used to store a list of Colon's separated directories that make up the command search path.

373
00:29:28,480 --> 00:29:34,570
You also saw how the path variable can affect what commands get executed and why that is along the way

374
00:29:34,570 --> 00:29:39,700
you were introduced to the which command as well as the base name and or name commands.

375
00:29:39,700 --> 00:29:44,810
Finally you learn how to create a for loop both at the command line as well as in a script.
