1
00:00:03,190 --> 00:00:09,490
Let's say we want to add two numbers together and assign them to a variable so we can do this like this

2
00:00:09,490 --> 00:00:13,710
will create a variable right here on the command line we'll call it.

3
00:00:14,180 --> 00:00:22,840
And we'll follow that with Dollar Sign opening parenthesis opening parenthesis and then an expression

4
00:00:22,840 --> 00:00:23,410
here.

5
00:00:23,410 --> 00:00:28,270
One plus two and we'll close that with these double parentheses here.

6
00:00:29,330 --> 00:00:36,710
So this dollar sign parenthesis parenthesis syntax is called arithmetic expansion bash evaluates the

7
00:00:36,710 --> 00:00:42,530
arithmetic expression within the double parentheses and it returns or substitutes the answer in its

8
00:00:42,530 --> 00:00:43,480
place.

9
00:00:43,490 --> 00:00:49,480
So now none with the variable knowm should contain the value of 3 and we can check it like so.

10
00:00:51,900 --> 00:00:56,290
And sure enough it does OK let's do some subtraction will set.

11
00:00:56,300 --> 00:01:00,810
No equal to 10 minus 1.

12
00:01:03,050 --> 00:01:08,720
And the value of that is nine multiplication is handled with an asterisk.

13
00:01:08,720 --> 00:01:13,390
So let's see that two times for

14
00:01:16,220 --> 00:01:17,460
is 8.

15
00:01:17,580 --> 00:01:29,840
Division is handled with a forward slash so we can do this.

16
00:01:29,920 --> 00:01:40,140
Let's talk about this particular situation so we'll do Six divided by four and then bash returns 1.

17
00:01:40,370 --> 00:01:44,580
So 6 divided by four is actually 1.5.

18
00:01:44,600 --> 00:01:47,540
But again Bash just returned one to us.

19
00:01:47,540 --> 00:01:49,590
So here's what you need to know.

20
00:01:49,610 --> 00:01:52,370
BASH doesn't do any rounding.

21
00:01:52,370 --> 00:01:56,290
It just drops off the decimal point and anything after it.

22
00:01:56,330 --> 00:02:00,020
So really bash doesn't support floating point arithmetic.

23
00:02:00,230 --> 00:02:04,910
That's usually fine because what you'll end up doing most of the time is very simple math like adding

24
00:02:04,910 --> 00:02:09,970
two numbers together or incrementing a number by one during a loop and things like that.

25
00:02:10,190 --> 00:02:15,140
If you need to work with numbers that contain a decimal then you're going to need to use an external

26
00:02:15,140 --> 00:02:19,880
program such as B C which stands for basic calculator.

27
00:02:19,970 --> 00:02:22,820
Now it might not be installed by default.

28
00:02:22,820 --> 00:02:29,690
So if you do this type dash A B C and you don't see it there then what you would need to do is do yum

29
00:02:30,140 --> 00:02:34,280
install this yes or dash y excuse me b c to install it.

30
00:02:34,490 --> 00:02:36,850
So here I have it installed but you may not.

31
00:02:36,860 --> 00:02:39,250
Again you may need to install it.

32
00:02:40,010 --> 00:02:43,460
So let's get some help on this thing here Dash 8.

33
00:02:43,460 --> 00:02:48,590
If you want to do floating point math with b c you have to turn on the math libraries with the dash

34
00:02:48,620 --> 00:02:49,850
l option.

35
00:02:49,850 --> 00:02:54,350
Also it's important to know that b c reads from standard input so you can do things like this.

36
00:02:54,350 --> 00:03:03,170
We can do echo Six divided by four and we'll pipe that into B C and we'll use the dash l option and

37
00:03:03,170 --> 00:03:09,080
it returns 1.5 by the way if you need a quick calculator at the command line you can just run B.C the

38
00:03:09,080 --> 00:03:16,340
shell and then do that here as well 6 for one plus one or whatever you want to do and then you can type

39
00:03:16,340 --> 00:03:19,440
Control D to exit out of the calculator here.

40
00:03:20,160 --> 00:03:22,410
Quickly you can also use awk to do this.

41
00:03:22,410 --> 00:03:28,080
It's a little bit more cumbersome but I'm just going to show you quickly how to do it and you can stack

42
00:03:28,080 --> 00:03:30,050
it away if you want to use it at some time ago.

43
00:03:30,220 --> 00:03:37,050
I recommend doing this but you may see this in other people's shellscript so we can use this arc again.

44
00:03:37,530 --> 00:03:43,780
And then what we want to do is print six divided by 4 and that is 1.5.

45
00:03:43,780 --> 00:03:48,710
So whether you're using B C or awk you get the full and complete answer.

46
00:03:48,700 --> 00:03:50,890
Now let's get back to bash.

47
00:03:50,890 --> 00:04:01,950
So Six divided by four according to bash is one.

48
00:04:01,980 --> 00:04:07,170
There is a remainder though when you do six divided by four and you can get that remainder where the

49
00:04:07,170 --> 00:04:08,220
percent sign.

50
00:04:08,280 --> 00:04:13,070
Also known as Maggiolo or the modulo operator so we can do this no.

51
00:04:13,140 --> 00:04:25,850
Equals 6 Maggiolo for and that is going to be 2 it prints 2 because 6 divided by 4 is 1 with 2 leftover

52
00:04:26,000 --> 00:04:28,580
or a remainder of 2.

53
00:04:28,580 --> 00:04:31,700
Now this comes in handy when you want to do alternating things.

54
00:04:31,700 --> 00:04:37,170
For example you could treat all even numbers one way and all odd numbers in a different way.

55
00:04:37,310 --> 00:04:43,400
Even numbers divided by two always have a remainder of 0 while odd numbers divided by 2 have a remainder

56
00:04:43,400 --> 00:04:44,210
of 1.

57
00:04:44,210 --> 00:04:49,170
Then you could use a simple IF OR Case statement to handle those different situations.

58
00:04:49,220 --> 00:04:53,360
By the way you don't have to use hardcoded numbers like I've been doing here.

59
00:04:53,360 --> 00:04:57,760
You can also use variables inside the parentheses too so we can do this something like this.

60
00:04:58,560 --> 00:05:12,330
Dice A is equal to three dice B is equal to six and the total of those dye would be TWICE A plus B

61
00:05:16,340 --> 00:05:19,430
and of course six plus three is nine.

62
00:05:19,490 --> 00:05:23,660
You might have noticed here that I didn't use a dollar sign for dice or dice B.

63
00:05:23,660 --> 00:05:26,190
Those are variables inside those double parentheses.

64
00:05:26,360 --> 00:05:30,350
You don't do that when you're using this particular syntax.

65
00:05:30,350 --> 00:05:36,350
By the way you can assign or change the value stored in a variable here you don't need the leading dollar

66
00:05:36,350 --> 00:05:42,200
sign that leading dollar sign is used for substitution but we don't want to substitute the command for

67
00:05:42,200 --> 00:05:44,820
its value we just want to change a variable.

68
00:05:44,840 --> 00:05:49,540
Hopefully just seeing it here will make more sense and to be clear than me trying to talk through it.

69
00:05:49,550 --> 00:05:56,610
So let's let's let's set Nahm equal to one and let's increment the value stored in the variable known

70
00:05:56,610 --> 00:06:01,760
by one and we do this by Prentice's parenthesis none plus plus.

71
00:06:01,830 --> 00:06:03,790
And then the closing parentheses here.

72
00:06:04,080 --> 00:06:06,330
And let's echo this.

73
00:06:06,330 --> 00:06:08,690
So now numb is two.

74
00:06:08,910 --> 00:06:14,670
So we've changed the variable without doing any output when we are using the double parentheses syntax.

75
00:06:14,670 --> 00:06:16,590
But it still affects the variable.

76
00:06:16,590 --> 00:06:24,060
Now let's decrement the value stored in the variable number by one and we can do this with a dash dash

77
00:06:24,060 --> 00:06:32,240
or minus minus and then two minus one is 1 and there we are we have one store in the variable name.

78
00:06:32,610 --> 00:06:38,360
Let's add a number to the value of the variable and store that new value in the variable.

79
00:06:38,550 --> 00:06:48,990
So we can do this knowm plus equals of 5 so 1 plus 5 or 6 and we assign that to none.

80
00:06:49,120 --> 00:06:51,240
And that is what is stored there.

81
00:06:51,250 --> 00:06:58,420
You can also do things like minus equal asterisk equal which is multiply or slash equal which is division

82
00:06:59,010 --> 00:07:01,120
Maggiolo equal and so on.

83
00:07:01,120 --> 00:07:06,640
Now if you wanted to you could actually use substitution to change the value of the same variable and

84
00:07:06,640 --> 00:07:07,540
it would look like this.

85
00:07:07,540 --> 00:07:11,690
We could do this number is equal to dollar sign.

86
00:07:11,710 --> 00:07:16,180
These parentheses num plus equals five

87
00:07:19,540 --> 00:07:22,680
and that is 11 because 6 plus 5 is 11.

88
00:07:22,690 --> 00:07:28,570
What this does is takes the output of the nonplus equals five operation and assigns it to the variable

89
00:07:28,570 --> 00:07:29,420
Nahm.

90
00:07:29,530 --> 00:07:32,700
If that seems clear to you you can do it that way as well.

91
00:07:32,710 --> 00:07:36,140
Or you can use the other syntax and I was just demonstrating.

92
00:07:36,460 --> 00:07:42,100
Now before we wrap up our discussion on math I want to show you two other ways to perform math operations

93
00:07:42,100 --> 00:07:44,640
that you might encounter in other scripts.

94
00:07:44,650 --> 00:07:49,480
The first way is with the let bash built in.

95
00:07:49,590 --> 00:07:52,710
You can see all the operators that are supported by doing this.

96
00:07:52,710 --> 00:07:59,340
We'll just get some help on it help let And we'll pipe that to less so you can see here the post-increment

97
00:07:59,340 --> 00:08:05,730
operator of plus plus the pre-increment plus plus that's before the variable name you know plus and

98
00:08:05,730 --> 00:08:08,430
minus the exponentiation.

99
00:08:08,430 --> 00:08:13,560
All these things that we're doing here and these things that we were doing here like plus equals minus

100
00:08:13,560 --> 00:08:14,960
equals and so on.

101
00:08:15,830 --> 00:08:20,660
Now this let syntax pretty much does the same thing as using double parenthesis.

102
00:08:20,660 --> 00:08:22,190
So let's do this.

103
00:08:22,340 --> 00:08:27,640
Let numb equals two plus three

104
00:08:30,750 --> 00:08:33,490
and sure enough that sets numb to five.

105
00:08:33,510 --> 00:08:39,520
You can also do this let numb plus plus and then increments it by 1.

106
00:08:39,510 --> 00:08:43,040
So now we have now with a value of six.

107
00:08:43,050 --> 00:08:49,260
The second way you'll see math performed in scripts is with DXP our command the double parentheses and

108
00:08:49,300 --> 00:08:51,350
actually been using is built into Bash.

109
00:08:51,570 --> 00:08:56,310
And that's the route I recommend if at all possible however you can rely on external programs like we

110
00:08:56,310 --> 00:09:05,440
saw earlier with B C so they you export command processes an expression given to it and then since that

111
00:09:05,440 --> 00:09:14,380
result to standard out so you expire one plus one it returns to now to do assignment.

112
00:09:14,390 --> 00:09:18,210
We just use command substitution like we would with any other command.

113
00:09:18,530 --> 00:09:24,040
So you know normal command substitution syntax

114
00:09:29,220 --> 00:09:32,890
and again two plus three returns of five that is assigned.

115
00:09:33,170 --> 00:09:37,530
And that is what is displayed when we access the value stored in that variable.

116
00:09:37,760 --> 00:09:38,170
OK.

117
00:09:38,210 --> 00:09:44,480
So that was a pretty long aside but we needed to cover it before we could get back to get ops and let's

118
00:09:44,480 --> 00:09:45,460
do that now.

119
00:09:48,330 --> 00:09:51,900
Get UPS does not change positional parameters.

120
00:09:51,920 --> 00:09:57,170
What this means is that the options are stored in dollar sign at Sign Dollar Sign one dollar sign too

121
00:09:57,170 --> 00:09:58,330
and so on.

122
00:09:58,340 --> 00:10:08,110
So let's add a bit of code into our skip to kind of prove this and pointed out.

123
00:10:08,250 --> 00:10:17,100
So right here after a while loop in case statement let's do this echo the number of arguments stored

124
00:10:17,200 --> 00:10:19,230
in dollar sign pound sign

125
00:10:22,970 --> 00:10:29,350
the contents of all the arguments is stored in our sign at sign.

126
00:10:29,420 --> 00:10:32,450
The first argument is stored in dollar sign one

127
00:10:36,540 --> 00:10:46,960
second argument is stored and positional parameters are assigned to.

128
00:10:46,960 --> 00:10:53,740
So now let's exit our script and see what this does so we'll do this all user DeMaio 11 Desha cell 8

129
00:10:54,250 --> 00:10:58,150
and we'll just type some extra stuff if you will at the end of the command line.

130
00:10:58,390 --> 00:11:07,480
So here we have three arguments dash S L is stored in dollar sign 1 8 is stored in Dollar Sign 2 and

131
00:11:07,570 --> 00:11:14,410
extra dash stuff is stored in dollar sign 3 in my mind once the arguments are processed they should

132
00:11:14,410 --> 00:11:16,770
be removed as a positional parameter.

133
00:11:16,780 --> 00:11:20,140
Now get ups doesn't do this for us but it gives us a way to do it.

134
00:11:20,140 --> 00:11:21,840
So let's jump back into our code

135
00:11:32,120 --> 00:11:33,980
get up sets a variable name.

136
00:11:34,190 --> 00:11:35,110
I n d.

137
00:11:35,390 --> 00:11:41,300
It stores the position of the next a command line argument following the options in that variable.

138
00:11:41,300 --> 00:11:43,850
So let's look at its value in action.

139
00:11:45,850 --> 00:11:52,540
So we'll run our same command here in OP I Indy is set to 3 because the third argument is the index

140
00:11:52,540 --> 00:11:59,260
or position where the arguments start after the options if we want to remove the options that have been

141
00:11:59,260 --> 00:12:00,010
processed.

142
00:12:00,010 --> 00:12:02,680
That means we need to shift them down by 2.

143
00:12:02,920 --> 00:12:07,840
So stated mathematically that Opt. I N D minus one.

144
00:12:07,840 --> 00:12:15,010
Now let's see if this little formula holds and some other examples so let's do this let's issues dash

145
00:12:15,070 --> 00:12:18,040
s and then some extra stuff.

146
00:12:18,070 --> 00:12:20,070
So sure enough OP I India's too.

147
00:12:20,080 --> 00:12:27,280
So if we subtract one from two that leaves us with one and if we shift by one then what's left is anything

148
00:12:27,280 --> 00:12:28,010
supplied.

149
00:12:28,030 --> 00:12:35,450
After the options now it's just run it with just some extra stuff without any options that we're checking

150
00:12:35,450 --> 00:12:36,170
for.

151
00:12:36,170 --> 00:12:40,920
Now in this case like I said there are no options and nothing needs to be shifted.

152
00:12:40,940 --> 00:12:46,650
However our little formula still works because up India's one one minus one is zero.

153
00:12:46,730 --> 00:12:53,450
And if we shift by zero we change nothing which is exactly what is needed for this case as well.

154
00:12:53,480 --> 00:13:09,270
So let's add this little bit of code or this formula or this algorithm if you will to our script.

155
00:13:09,450 --> 00:13:21,270
So we're going to shift by the number of opt in D minus one.

156
00:13:21,290 --> 00:13:27,880
Now we can process dollar sign at sign dollar sign $1 sign too and so on just like we've always done.

157
00:13:27,920 --> 00:13:35,300
Now let's display the contents of dollar sign at sign Xandra after the shift so we'll do this Ecko after

158
00:13:35,300 --> 00:13:36,530
the shift.

159
00:13:37,800 --> 00:13:45,090
And we'll just grab all these up here and print them to our screen again.

160
00:13:45,110 --> 00:13:48,470
So now let's use our user Dymo 11. sh.

161
00:13:48,530 --> 00:13:51,830
SL 8 extra dash stuff.

162
00:13:51,890 --> 00:13:57,230
So sure enough that works the number of arguments we have at the beginning is three again dash SL is

163
00:13:57,230 --> 00:14:03,350
stored in Dallas on one dollar sign two is a dollar sign three evaluates to extra stuff.

164
00:14:03,350 --> 00:14:08,720
So after get UPSs done and we process all those arguments the only thing we're left with is extra dash

165
00:14:08,720 --> 00:14:14,410
stuff which have to we do our shift is assigned to dollar sign one and that's exactly what we want.

166
00:14:14,420 --> 00:14:20,300
So then we can do things like loop through all the other remaining arguments on the command line with

167
00:14:20,300 --> 00:14:23,680
a while loop for example while dollar at sign.

168
00:14:23,750 --> 00:14:30,260
We could then make people do things like after all your options then we require you to give us a file

169
00:14:30,470 --> 00:14:35,510
or a username or whatever that we expect now assign one then we can test and do some work against dollar

170
00:14:35,510 --> 00:14:38,140
sign one and so on and so forth.

171
00:14:38,450 --> 00:14:44,150
However let's say in this particular script we want to treat anything other than the options on the

172
00:14:44,150 --> 00:14:45,990
command line as an error.

173
00:14:46,040 --> 00:14:49,250
And the easiest way to do that is to test the dollar sign pounce on value.

174
00:14:49,250 --> 00:14:53,600
So let's jump back into our script and say if

175
00:14:59,260 --> 00:15:04,720
if the number of arguments is greater than zero then we're going to give them the usage statement and

176
00:15:04,720 --> 00:15:09,850
we'll get out of here again we're going to reduce the usage function since it teaches the user how to

177
00:15:09,850 --> 00:15:11,490
use the script the way we want them to.

178
00:15:11,500 --> 00:15:14,790
And then it exits with a non zero exit status.

179
00:15:14,860 --> 00:15:20,620
And remember when you use shift the number of arguments changes and that also updates the dollar sign

180
00:15:20,620 --> 00:15:23,570
pound sign variable so this is going to work.

181
00:15:23,590 --> 00:15:27,950
Now before we test our updated script let's clean up our bugging and development code here all these

182
00:15:27,950 --> 00:15:31,390
all echo statements and make sure everything is all cleaned up.

183
00:15:31,390 --> 00:15:32,630
So let me go up here.

184
00:15:34,000 --> 00:15:35,230
We'll get rid of that

185
00:15:37,740 --> 00:15:40,820
and we don't need these lines as well.

186
00:15:49,440 --> 00:15:52,850
Now let's do this let's run it with an argument.

187
00:15:53,010 --> 00:15:58,650
So here it gives us the usage just like we want or we exit with a non-zero exit status because it's

188
00:15:58,770 --> 00:16:03,600
in that usage function that we created at the top of our script.

189
00:16:03,660 --> 00:16:05,100
So let's do something like this.

190
00:16:05,100 --> 00:16:11,950
Let's say we'll pass a valid option and then also append some stuff to the end of the line here.

191
00:16:12,210 --> 00:16:17,460
And sure enough it gets to that point we're looking at the number of arguments after we've processed

192
00:16:17,460 --> 00:16:19,080
the command line options.

193
00:16:19,260 --> 00:16:24,470
And that has triggered the calling of the usage function as well.

194
00:16:24,480 --> 00:16:29,650
So let's go ahead and do this with something that is proper A dash s.

195
00:16:29,820 --> 00:16:35,080
And sure enough we get our password with a special character appended to it.

196
00:16:35,100 --> 00:16:37,010
Let's do a quick recap.

197
00:16:37,170 --> 00:16:42,060
And this lesson you learn how to use get up to process command line options.

198
00:16:42,060 --> 00:16:47,160
You learn how to wrap the get ups command in a while loop and use a case statement to perform different

199
00:16:47,190 --> 00:16:53,490
actions for each option when you want an option to take an argument you follow that option letter with

200
00:16:53,490 --> 00:16:56,340
a colon in the OP's string.

201
00:16:56,340 --> 00:17:01,020
You also learn how to strip away the options from the set of positional parameters by using the OPT

202
00:17:01,060 --> 00:17:04,130
I and the variable provided by get ups.

203
00:17:04,260 --> 00:17:07,200
Finally you learn how to perform mathematical operations.

204
00:17:07,320 --> 00:17:12,180
You learned that you could assign the result of an arithmetic evaluation to a variable by using the

205
00:17:12,180 --> 00:17:14,570
dollar sign double parentheses syntax.

206
00:17:14,820 --> 00:17:20,590
In addition to hard coding numbers in your expressions You also performed arithmetic using variables

207
00:17:21,050 --> 00:17:26,580
you even learned how to change the value stored in the variable directly using a few different operators

208
00:17:26,580 --> 00:17:31,020
including the increment decrement and the plus equal operators.
