1
00:00:03,000 --> 00:00:09,140
In this lesson will continue exploring positional parameters and you'll also learn how to create a while

2
00:00:09,140 --> 00:00:09,800
loop.

3
00:00:10,130 --> 00:00:17,270
Horses always I'm going to start a terminal on my system and move into the folder for the class and

4
00:00:17,270 --> 00:00:23,870
then move into the folder for the project that I'm working on and then start the virtual machine.

5
00:00:24,110 --> 00:00:26,030
And when it comes up we'll connect to it.

6
00:00:33,220 --> 00:00:37,720
Now that we're connected to our virtual machine will move into the shared folder a Ford slash a vagrant

7
00:00:38,200 --> 00:00:47,970
and this script I'm going to name all user Dymo 7. SH course we're going to start out our script with

8
00:00:47,970 --> 00:00:51,660
the shebang and give it a path to the bash interpreter.

9
00:00:51,660 --> 00:00:59,910
I'm also going to provide a little header here demonstrate the use of shift the shift command and while

10
00:00:59,910 --> 00:01:06,240
loops and a previous lesson you learned that the first positional parameter of dollar signs 0 contains

11
00:01:06,240 --> 00:01:08,020
the name of the script itself.

12
00:01:08,100 --> 00:01:13,170
While dollar sign one stores the value of the first argument passed to the script on the command line

13
00:01:13,530 --> 00:01:16,410
dollar sign to stores a second argument and so on.

14
00:01:16,440 --> 00:01:21,890
We never actually used dollar sign one dollar sign to directly in that lesson.

15
00:01:22,080 --> 00:01:28,550
So let's do that now so you get a chance to see how that works and how they behave in different situations.

16
00:01:29,010 --> 00:01:33,020
So let's just display the first three parameters.

17
00:01:35,050 --> 00:01:36,730
Well use an echo statement here.

18
00:01:40,450 --> 00:01:43,860
And I'm just going to copy and paste

19
00:01:51,960 --> 00:01:57,610
going to add an echo statement here which is just going to print a blank line.

20
00:01:57,630 --> 00:02:00,630
Now I'm going to save the changes to the script.

21
00:02:01,950 --> 00:02:03,180
And then exit.

22
00:02:03,300 --> 00:02:12,890
Since this is the first time I'm executing this script I need to make sure that it has the proper permissions.

23
00:02:12,890 --> 00:02:16,500
Now I'm just going to execute the script without any arguments.

24
00:02:16,550 --> 00:02:22,940
So there are no arguments and that means there are no values to store inside the script as positional

25
00:02:22,940 --> 00:02:23,630
parameters.

26
00:02:23,750 --> 00:02:31,420
So of course parameter 1 2 and 3 are blank let's pass in one parameter and see what happens we'll just

27
00:02:31,690 --> 00:02:34,030
call it with an argument of Apple.

28
00:02:34,030 --> 00:02:38,950
So now dollar sign one is assigned the value of Apple and that gets printed.

29
00:02:38,950 --> 00:02:43,190
So let's do this again with bread it's assigned to dollar sign too.

30
00:02:43,540 --> 00:02:45,500
And let's say Candy.

31
00:02:45,610 --> 00:02:51,820
So now dollar sign one is Apple dollar sign to his bread dollar sign three is Candy and we can directly

32
00:02:51,880 --> 00:02:56,590
access those and use those in the script by using dollar sign one and so on.

33
00:02:57,310 --> 00:03:02,620
If we were to add more command line arguments for example another word here let's say dumplings then

34
00:03:02,650 --> 00:03:04,620
that is stored in dollar sign for.

35
00:03:04,750 --> 00:03:07,960
But we didn't account for a user entering dollar sign for.

36
00:03:07,960 --> 00:03:14,290
So that presents a challenge and so that's why we're going to use things like for loops and while loops

37
00:03:14,290 --> 00:03:17,580
to handle command line arguments in many cases.

38
00:03:17,590 --> 00:03:23,230
Next in our script we want to loop through all the positional parameters and the previous lesson we

39
00:03:23,230 --> 00:03:24,610
used a for loop to do that.

40
00:03:24,610 --> 00:03:27,810
But this time let's use a while loop.

41
00:03:27,910 --> 00:03:29,530
So let's look at the while command.

42
00:03:29,530 --> 00:03:30,750
What type is it.

43
00:03:30,790 --> 00:03:33,360
Well it's a shell keyword or shell built in.

44
00:03:33,490 --> 00:03:39,130
So that means we can use help against that command help a while and hit enter to get some information

45
00:03:39,130 --> 00:03:40,430
about it.

46
00:03:40,450 --> 00:03:48,570
Now that says while commands do commands done this executes commands as long as the final command in

47
00:03:48,570 --> 00:03:51,520
the while commands has an exit status of 0.

48
00:03:51,720 --> 00:03:58,890
You can think of this as being while true do while the condition that is being tested for is true then

49
00:03:58,890 --> 00:04:00,630
the commands will execute.

50
00:04:00,630 --> 00:04:06,210
This means that during the while loop you want to change the thing being tested for in some way so that

51
00:04:06,210 --> 00:04:11,560
the condition eventually evaluates to false or you'll end up in an infinite loop.

52
00:04:12,320 --> 00:04:15,060
Let's create a while loop here at the command line.

53
00:04:15,080 --> 00:04:20,160
Let's use a variable called X will assign that the value of one will use.

54
00:04:20,180 --> 00:04:25,740
While we're going to use our double brackets just like we did in the for loop.

55
00:04:26,180 --> 00:04:30,770
Now we're going to use a conditional expression we're going to test a half dollar sign.

56
00:04:30,770 --> 00:04:33,650
X is equal to 1.

57
00:04:34,190 --> 00:04:37,320
If that is true then what we're going to do is echo.

58
00:04:37,370 --> 00:04:44,280
This is the value of x and then simply Ecko the value of x.

59
00:04:44,280 --> 00:04:49,980
Now we're just going to manipulate the variable x in some way and we'll just change it to 7 here and

60
00:04:49,980 --> 00:04:51,220
then type done.

61
00:04:51,360 --> 00:04:56,580
And so what happens is while and that conditional expression X equals 1.

62
00:04:56,760 --> 00:05:01,250
If that's true and it is then it executes the code block between do and done.

63
00:05:01,440 --> 00:05:05,640
So it echoes what you see on your screen and then X is changed to 7.

64
00:05:05,670 --> 00:05:07,400
Now the while loop executes again.

65
00:05:07,410 --> 00:05:09,710
So it says now is X equal to 1.

66
00:05:09,780 --> 00:05:16,620
Well it is now false because X has 7 so it doesn't execute the commands below and the while loop exits.

67
00:05:16,800 --> 00:05:21,810
This loop isn't really practical but it demonstrates changing a variable that is being tested against

68
00:05:22,070 --> 00:05:24,590
in the conditional expression of the While loop.

69
00:05:24,720 --> 00:05:29,910
By the way if the conditional expression never evaluates to true then the commands in the while loop

70
00:05:30,090 --> 00:05:31,930
will never get executed.

71
00:05:31,950 --> 00:05:38,510
For example right now the value of x is 7 so we can do Ekko dollar sign X sure enough at 7.

72
00:05:38,760 --> 00:05:44,890
And if we execute a while loop again this back up here a couple of commands and hit enter.

73
00:05:44,910 --> 00:05:50,650
Nothing happens because X dash Q 1 evaluates to false.

74
00:05:51,060 --> 00:05:55,470
When you're learning about the wild loop and doing some experiments on your own you're going to end

75
00:05:55,470 --> 00:05:59,550
up creating an infinite loop at some point it's just going to happen.

76
00:05:59,700 --> 00:06:05,790
So let's do it on purpose and show how easy it is to break out of it before we do then I want to introduce

77
00:06:05,790 --> 00:06:07,610
you to the true command.

78
00:06:07,620 --> 00:06:11,510
It's only job is to return an exit status of 0.

79
00:06:11,790 --> 00:06:13,570
So what type of command is true.

80
00:06:15,120 --> 00:06:18,500
It is a built in as well as a program.

81
00:06:18,600 --> 00:06:21,500
Of course we can get help on a built in by using help.

82
00:06:21,810 --> 00:06:26,760
True and it says it just returns a successful result.

83
00:06:26,760 --> 00:06:29,340
The exit status is that it always succeeds.

84
00:06:29,340 --> 00:06:34,590
Now I happen to like the description of the executable better so let's look at the man page for user

85
00:06:34,590 --> 00:06:36,250
been true all this to man.

86
00:06:36,420 --> 00:06:36,940
True.

87
00:06:37,110 --> 00:06:39,980
And it says do nothing successfully.

88
00:06:39,990 --> 00:06:41,770
I just love that definition.

89
00:06:41,790 --> 00:06:48,240
So in any case no matter what happens with true it just returns an exit status of 0.

90
00:06:48,270 --> 00:06:54,580
So we can test this we'll just type the true command and then we can look at Ekko dollar sign questionmark

91
00:06:54,660 --> 00:06:56,740
sure enough it returns 0.

92
00:06:56,850 --> 00:07:04,350
If we do true blah it doesn't really matter because again it just always returns zero before we create

93
00:07:04,350 --> 00:07:11,860
our infinite loop here let's look at just one more command called Sleep sleep happens to be an executable

94
00:07:11,860 --> 00:07:12,910
program on the system.

95
00:07:12,910 --> 00:07:14,400
It's not a shell built in.

96
00:07:14,440 --> 00:07:21,080
So we'll use the man page to learn more about sleep so sleep simply delays execution for a specified

97
00:07:21,080 --> 00:07:22,150
amount of time.

98
00:07:22,280 --> 00:07:27,530
You can use sleep followed by a number and a suffix and that suffix can be asked for seconds and for

99
00:07:27,530 --> 00:07:30,260
minutes for hours or four days.

100
00:07:30,500 --> 00:07:35,980
So sleep does what it sounds like it just sleeps it doesn't do anything for a specific amount of time.

101
00:07:36,050 --> 00:07:41,420
So I get to get out of the man page here and then just to sleep for one second.

102
00:07:41,810 --> 00:07:44,300
Nothing happens and you return to the command line.

103
00:07:44,420 --> 00:07:51,670
Of course that is the same as sleep one ass and you can even do half a second or portions of a second.

104
00:07:51,680 --> 00:07:52,870
So let's do this sleep.

105
00:07:52,940 --> 00:07:54,900
We're just a half a second and hit enter.

106
00:07:55,980 --> 00:08:03,340
Now let's build our infinite loop so we can do while and we'll test for true true always returns 0 and

107
00:08:03,360 --> 00:08:04,500
exit status of zero.

108
00:08:04,500 --> 00:08:06,150
So it's always true.

109
00:08:06,150 --> 00:08:11,130
So this is going to never change this while loop is going to run for ever.

110
00:08:11,370 --> 00:08:13,280
So we'll just do while true.

111
00:08:13,410 --> 00:08:18,000
Do then what we're going to do is just simply echo a random number

112
00:08:22,020 --> 00:08:28,050
and now we're going to sleep for one second and then we'll close out our while loop here with the syntax

113
00:08:28,110 --> 00:08:29,390
of done.

114
00:08:29,430 --> 00:08:35,100
Now when we hit enter Echo gets executed it executes and displays a random number.

115
00:08:35,100 --> 00:08:37,350
Sleep rest for a second if you will.

116
00:08:37,350 --> 00:08:41,850
And then the loop repeats because while true always evaluates to true.

117
00:08:42,000 --> 00:08:44,520
So this will just go on forever.

118
00:08:46,030 --> 00:08:51,970
Now to break out of this loop all you have to do is hold down the control key and type C so Control

119
00:08:52,000 --> 00:08:56,110
C sends an interrupt signal to the command which makes it stop.

120
00:08:56,110 --> 00:09:01,090
Now this isn't something you need to the while command you can use this to stop any command running

121
00:09:01,090 --> 00:09:02,520
in the foreground.

122
00:09:02,560 --> 00:09:07,450
For example if you execute sleep and want to interrupt it and return to the command line sooner you

123
00:09:07,450 --> 00:09:08,430
can use control.

124
00:09:08,440 --> 00:09:10,260
See there as well.

125
00:09:10,570 --> 00:09:16,690
So let's say we give sleep the instruction to sleep for 10 minutes and then we decide we don't want

126
00:09:16,690 --> 00:09:21,760
to wait that long so we'll just go ahead and type Control-C and it breaks out of the command here on

127
00:09:21,760 --> 00:09:23,110
the screen you can see the care.

128
00:09:23,110 --> 00:09:27,590
See that shows that indeed these commands were interrupted with control.

129
00:09:27,640 --> 00:09:34,300
See there's one more small piece of the puzzle left to cover before we use a while loop in our script

130
00:09:34,690 --> 00:09:37,350
to loop over all the positional parameters.

131
00:09:37,360 --> 00:09:43,240
This last piece of the puzzle is the shift the built in the type of a shift.

132
00:09:43,240 --> 00:09:44,520
It's a shell built in.

133
00:09:44,560 --> 00:09:46,300
Let's get some help on this here.

134
00:09:47,430 --> 00:09:52,920
It says it renames the positional parameters of N plus 1 and plus 2 dollars on wandless onto and so

135
00:09:52,920 --> 00:09:54,000
on.

136
00:09:54,000 --> 00:09:59,960
Now what the shift command really does is that it removes positional parameters from the list starting

137
00:09:59,970 --> 00:10:03,350
with the dollar sign when it just chops off dollar sign one.

138
00:10:03,510 --> 00:10:07,880
So when you execute the shift command the value stored in dollar sign one is removed.

139
00:10:07,890 --> 00:10:09,240
It's just gone.

140
00:10:09,240 --> 00:10:15,840
What also happens is that the value stored in dollar sign to shift down to dollar sign one dollar sign

141
00:10:15,840 --> 00:10:21,840
three replaces the value in dollar sign to four replaces the value in three and so on and so forth.

142
00:10:21,900 --> 00:10:27,700
Also the special parameter of a dollar sign pound sign is reduced to buy one.

143
00:10:27,720 --> 00:10:32,910
Now as you remember a dollar sign Palestine contains the number of arguments that were supplied on the

144
00:10:32,910 --> 00:10:34,590
command line by the fall.

145
00:10:34,620 --> 00:10:36,430
Shift shifts everything.

146
00:10:36,450 --> 00:10:43,440
One place executing shift by itself is the same thing as executing shift space one if you want to shift

147
00:10:43,470 --> 00:10:46,510
everything by two places use shift to.

148
00:10:46,530 --> 00:10:50,220
In that case dollar sign pound sign will be decreased by two.

149
00:10:50,460 --> 00:10:54,310
If you want to shift everything by three positions you shift three and so on.

150
00:10:54,540 --> 00:10:59,550
Now if this sounds confusing Don't worry it's actually harder to explain than it is to demonstrate So

151
00:10:59,700 --> 00:11:03,300
when you see it being used in a script it will make total sense.

152
00:11:03,300 --> 00:11:04,590
So let's do that right now.

153
00:11:04,590 --> 00:11:06,870
Let's use a shift in our script.

154
00:11:11,840 --> 00:11:20,550
So we'll loop through all the positional parameters.

155
00:11:20,750 --> 00:11:27,020
We'll use a while loop while double brackets here again make sure there are spaces in between while

156
00:11:27,070 --> 00:11:30,060
under double brackets and a space after the double brackets.

157
00:11:30,080 --> 00:11:34,190
We're going to test for a dollar sign pound sign here.

158
00:11:34,430 --> 00:11:38,780
The number of parameters and we'll say if it's greater than zero.

159
00:11:38,810 --> 00:11:43,970
That means if there were any arguments on the command line then this will evaluate to true and that

160
00:11:43,970 --> 00:11:49,690
point what we want to do is echo the number of parameters.

161
00:11:51,340 --> 00:11:57,820
Is a dollar sign pound sign and then will echo the first parameter here.

162
00:12:03,250 --> 00:12:06,860
And we're just going to repeat this a couple more times here.

163
00:12:07,090 --> 00:12:17,390
So will echo a dollar sign too and a dollar sign three then will echo a blank line and then what we're

164
00:12:17,390 --> 00:12:20,780
going to do is use the Shift the built in command.

165
00:12:21,080 --> 00:12:27,020
So just like we said shift is actually going to modify dollar sign pound sign because when it's executed

166
00:12:27,320 --> 00:12:32,260
it removes one parameter from the beginning dollar sign one is gone it shifts everything down.

167
00:12:32,270 --> 00:12:35,930
So that means at that point the number of positional parameters has changed.

168
00:12:36,110 --> 00:12:38,920
And Dollar Sign pound sign has changed to reflect that.

169
00:12:39,020 --> 00:12:44,510
So we are modifying the condition that we are testing for through this loop so we shouldn't end up in

170
00:12:44,510 --> 00:12:48,530
an infinite loop here or some going to end this with done.

171
00:12:48,800 --> 00:12:51,710
Save my changes and then execute the script.

172
00:12:53,980 --> 00:12:59,840
So if we execute the script without any arguments then there are no positional parameters to loop through.

173
00:13:00,160 --> 00:13:05,140
So that's why we only get the top portion of the script displaying some output but we don't get any

174
00:13:05,140 --> 00:13:08,230
of the commands in the while loop being executed.

175
00:13:08,230 --> 00:13:10,630
Now let's apply just one argument here.

176
00:13:11,680 --> 00:13:14,940
That caused the while loop to execute one time.

177
00:13:14,950 --> 00:13:20,690
So dollar sign one is assigned to Apple then shift occurs and so that removes dollar sign one to shift

178
00:13:20,710 --> 00:13:21,600
everything down.

179
00:13:21,790 --> 00:13:23,590
Well there's nothing left to shift down.

180
00:13:23,710 --> 00:13:27,550
So dollar sign pound sign evaluates to 0 0 0.

181
00:13:27,580 --> 00:13:29,700
Greater than zero is false.

182
00:13:29,860 --> 00:13:33,230
So the while loop stops executing because it's false.

183
00:13:33,250 --> 00:13:37,660
So let's go ahead and add a nother argument on the command line and see what happens.

184
00:13:37,660 --> 00:13:39,160
So we'll add bread.

185
00:13:39,490 --> 00:13:45,930
So the first time the loop gets executed dollar sign one contains an apple dollar sign to contains bread.

186
00:13:46,030 --> 00:13:47,860
The shift command is executed.

187
00:13:47,860 --> 00:13:53,190
Apple blows away the original dollar sign one goes away gets lopped off the beginning of the list.

188
00:13:53,190 --> 00:13:58,570
Their bread which is stored in dollar sign to then becomes a dollar sign one.

189
00:13:58,750 --> 00:14:00,580
And there are no positional parameters.

190
00:14:00,580 --> 00:14:01,730
Two and Three.

191
00:14:01,750 --> 00:14:05,110
So let's keep doing this and you can clearly see what's going to happen.

192
00:14:05,110 --> 00:14:11,050
We'll do candy at the outset of the script dollar sign one is Apple dollar sign too is bread dollar

193
00:14:11,060 --> 00:14:16,560
sign three is Candy the first time the while loop is executed and shift gets executed.

194
00:14:16,660 --> 00:14:19,330
The number of parameters gets decreased by 1.

195
00:14:19,330 --> 00:14:23,970
So then there are only two parameters bread and candy bread being dollar sign one candy being two.

196
00:14:24,270 --> 00:14:29,110
While Luke gets executed again bread gets chopped off the beginning there and you're only left with

197
00:14:29,110 --> 00:14:33,550
candy which then becomes dollar sign one the shift command is executed again.

198
00:14:33,550 --> 00:14:39,010
Candy goes away and there are no more positional parameters so zero greater than zero is false in the

199
00:14:39,010 --> 00:14:40,790
while loop exits.

200
00:14:41,140 --> 00:14:43,240
So let's do this again with one more argument.

201
00:14:44,690 --> 00:14:49,940
So as you can clearly see every time we go through the while loop and the shift command is executed

202
00:14:49,970 --> 00:14:56,010
everything shifts down by one to the while loop executes four times and then exits.

203
00:14:56,030 --> 00:15:00,590
It might not be obvious when you would want to use the shift command beyond our while loop that we've

204
00:15:00,590 --> 00:15:01,920
demonstrated here.

205
00:15:02,120 --> 00:15:05,330
So here are a couple of thoughts to keep in mind.

206
00:15:05,420 --> 00:15:10,940
One time shift comes in handy is when you want to have a user supply information as arguments to your

207
00:15:10,940 --> 00:15:16,900
script and there is one argument or piece of data that can contain multiple words.

208
00:15:17,060 --> 00:15:22,240
For instance data you would want to use in the comment field when creating a user.

209
00:15:22,370 --> 00:15:27,890
In this case you would tell the user to supply a username which would be dollar sign one then you would

210
00:15:27,980 --> 00:15:32,600
shift and then what is left over becomes the comments field.

211
00:15:32,610 --> 00:15:34,820
Now assign 1 2 and 3 and so on.

212
00:15:34,820 --> 00:15:36,560
Now that's one example.

213
00:15:36,560 --> 00:15:39,520
Also think about processing command line arguments.

214
00:15:39,520 --> 00:15:44,020
For example something like this dash dash dash for Beauce.

215
00:15:44,300 --> 00:15:49,520
So just keep these ideas in mind and will be coming back to them in future lessons and exercises.

216
00:15:49,880 --> 00:15:56,420
To quickly recap in this lesson you used a positional parameter starting with dollar sign one explicitly.

217
00:15:56,510 --> 00:16:00,240
You review the special variable of dollar sign pound sign.

218
00:16:00,290 --> 00:16:04,490
You are also introduced to the true sleep and shift commands.

219
00:16:04,500 --> 00:16:08,630
Finally you put all this new information together in order to create a while loop.
