WEBVTT - This file was automatically generated by VIMEO

0
00:00:10.240 --> 00:00:11.650
Welcome to section five.

1
00:00:12.110 --> 00:00:14.890
In this section you will learn about scripting basics using Bash.

2
00:00:15.360 --> 00:00:18.370
This section is not meant to be a definitive guide to bash however,

3
00:00:18.950 --> 00:00:21.730
but rather appointed approach only providing what you'll need.

4
00:00:22.490 --> 00:00:25.710
You'll start off by learning bash fundamentals like data structures,

5
00:00:25.740 --> 00:00:27.270
flow control and variables.

6
00:00:27.780 --> 00:00:30.670
Then you will take that knowledge and create some administrative scripts.

7
00:00:32.360 --> 00:00:35.740
In this video you'll learn about Bash and how to make a bash script.

8
00:00:37.580 --> 00:00:40.000
Our plan is to explore the fundamentals of the Bash shell.

9
00:00:40.290 --> 00:00:42.400
We'll learn about common paradigms like arrays,

10
00:00:42.400 --> 00:00:47.260
flow control and functions by reviewing the provided code samples. Finally,

11
00:00:47.320 --> 00:00:49.900
you will create a game and bash with some of the knowledge you have just

12
00:00:49.900 --> 00:00:54.250
learned. Okay, let's get started. Okay,

13
00:00:54.360 --> 00:00:56.330
here we are back in the terminal again.

14
00:00:57.150 --> 00:01:02.040
And so you've been using Bash this whole time and maybe you recognize

15
00:01:02.040 --> 00:01:02.780
that maybe you didn't.

16
00:01:02.780 --> 00:01:07.710
So Bash is just a shell that you use when you log in and to be able to

17
00:01:07.870 --> 00:01:10.510
interact with your os. It's also a command line interpreter.

18
00:01:11.330 --> 00:01:15.310
You could run commands in the Bash like Ls or CD like you already have,

19
00:01:15.450 --> 00:01:19.070
or you could put them into a script and run that, which is what we're gonna do.

20
00:01:20.010 --> 00:01:23.710
So I've included all the scripts in the GitHub repo for this section under

21
00:01:23.710 --> 00:01:28.350
section five. You can grab them by installing Git on your e C two instance.

22
00:01:28.350 --> 00:01:29.470
So if you're unfamiliar with Git,

23
00:01:29.650 --> 00:01:32.270
it is a distributed version source control system.

24
00:01:32.770 --> 00:01:36.550
And what we're gonna do is we are gonna clone uh, the repo,

25
00:01:36.550 --> 00:01:39.310
which you obviously already cloned it onto your local host.

26
00:01:39.480 --> 00:01:43.190
We're gonna clone it here on our EC two instance. So to do that,

27
00:01:43.440 --> 00:01:48.330
let's first start out by installing Git and we'll have to use pseudo again cuz

28
00:01:48.330 --> 00:01:52.470
we need elevated privileges and we're gonna say Yum, install Git.

29
00:01:53.510 --> 00:01:55.090
And then after that just hit enter

30
00:01:56.590 --> 00:02:00.930
and gonna ask us are we sure it's gonna install these dependencies? We are.

31
00:02:04.570 --> 00:02:05.820
Okay, we're installed,

32
00:02:06.120 --> 00:02:10.620
we clear the screen and so now we have the Git command line. As you can see,

33
00:02:10.620 --> 00:02:14.300
I just tested it and we didn't give anything so it just gave us the help.

34
00:02:15.350 --> 00:02:15.870
All right,

35
00:02:15.870 --> 00:02:19.170
I'm gonna go ahead and paste this in so you can go ahead and clone it.

36
00:02:19.310 --> 00:02:21.730
I'm sure you have it for when you did the beginning of this course.

37
00:02:22.190 --> 00:02:25.330
So I'm gonna pause the video here, let it clone cuz uh,

38
00:02:25.330 --> 00:02:28.970
it may take a little bit when I come back we'll have it already on the system.

39
00:02:30.790 --> 00:02:35.760
Okay, so I cloned it. As you can see here, I ran LS and we are all set.

40
00:02:35.860 --> 00:02:37.800
It is on our e C two instance.

41
00:02:38.260 --> 00:02:41.960
And the biggest reason for this is this is just gonna be easier for us to review

42
00:02:42.100 --> 00:02:46.000
the source code examples here because this course is supposed to get you up and

43
00:02:46.000 --> 00:02:47.160
running as fast as possible.

44
00:02:47.710 --> 00:02:52.000
It's not advantageous for us to go and write every single one of these scripts

45
00:02:52.000 --> 00:02:52.920
by hand to learn bash.

46
00:02:52.950 --> 00:02:56.160
That would be great for a course that was explicitly made just for Bash.

47
00:02:56.260 --> 00:02:58.600
But since we're just trying to get uh, some foundation stuff,

48
00:02:59.150 --> 00:03:02.480
I've took the liberty of adding those examples like our, of our,

49
00:03:02.480 --> 00:03:03.313
like I said earlier,

50
00:03:03.620 --> 00:03:06.680
and we're just gonna review them and I'm gonna go over some highlights of what I

51
00:03:06.680 --> 00:03:08.800
think is important about Bash that you should take away.

52
00:03:10.170 --> 00:03:14.670
At the end of this video we are gonna write a a script together. Um,

53
00:03:14.670 --> 00:03:17.670
and we'll do that from scratch. Okay, so let's get started.

54
00:03:17.850 --> 00:03:21.470
So one of the things you're gonna need for this video as well is you're gonna

55
00:03:21.470 --> 00:03:26.310
need an editor. I'm gonna use the editor right here on my e c two instance.

56
00:03:26.410 --> 00:03:30.390
I'm gonna use Vim, which v i m if you're unfamiliar. And um,

57
00:03:30.460 --> 00:03:34.830
there's also nano available N A N O. But I'll leave it to you which you prefer.

58
00:03:35.250 --> 00:03:36.430
You can't go wrong with either one.

59
00:03:36.650 --> 00:03:38.950
If you're unsure how to use either one of those editors,

60
00:03:39.050 --> 00:03:43.070
please pause the video now and uh, go ahead and do some research before uh,

61
00:03:43.070 --> 00:03:44.600
proceeding. Okay,

62
00:03:44.780 --> 00:03:48.040
so the first thing that we're gonna do here is we are going to open

63
00:03:50.550 --> 00:03:53.930
the dummy sh file under this section five directory.

64
00:03:55.420 --> 00:03:59.680
And so this is a file we're gonna go over some quick basics of bash

65
00:04:00.350 --> 00:04:03.760
that don't kind of fit into the other uh, data structures and flow control.

66
00:04:04.400 --> 00:04:06.340
But this is also, I'll give you an exam, uh, uh,

67
00:04:06.860 --> 00:04:09.140
a chance to go ahead and like test your editor out as well.

68
00:04:09.400 --> 00:04:10.660
So let me open mind and vim.

69
00:04:10.660 --> 00:04:14.870
So I type vim and then the file wanna open that hit return. Okay,

70
00:04:14.890 --> 00:04:18.670
so line one here and this is the first thing that should jump out it to you.

71
00:04:19.410 --> 00:04:23.110
The first line is what we call a shebang.

72
00:04:23.530 --> 00:04:28.490
And so the shebang in any type of of scripting basically tells

73
00:04:28.490 --> 00:04:31.170
the os how it wants to interpret the rest of this file.

74
00:04:31.390 --> 00:04:35.890
And in our case we want the rest of this file to be interpreted by Bash. Um,

75
00:04:35.890 --> 00:04:38.290
obviously we're using bash commands so that makes sense.

76
00:04:38.830 --> 00:04:43.610
So you can also notice that this line starts out with a pound sign

77
00:04:44.610 --> 00:04:47.670
and you can see those, these are in bash are comments.

78
00:04:47.670 --> 00:04:50.670
This is how you make a comment. You can see here on line three I made a comment.

79
00:04:51.500 --> 00:04:55.880
So those will be ignored by the bash interpreter. You get down to line five.

80
00:04:55.880 --> 00:04:59.240
Here we have what's Echo and then we just say Hello everyone.

81
00:04:59.700 --> 00:05:03.040
And the echo command is basically how to print something to the screen.

82
00:05:03.980 --> 00:05:06.960
We go down here to line seven and is an exit command.

83
00:05:07.060 --> 00:05:11.160
So in Bash you will want to use a lot of exit commands, um,

84
00:05:11.160 --> 00:05:15.720
because if your script needs is not doing something there was an error needs to

85
00:05:15.720 --> 00:05:17.200
get out of the script early,

86
00:05:17.300 --> 00:05:21.640
you can use the exit command and this way you will also allow to alert the

87
00:05:21.640 --> 00:05:26.120
caller that there was a problem in Bash and in in Unix and Lennox General.

88
00:05:26.760 --> 00:05:30.450
Usually something when it exit zero is means everything was great. We don't,

89
00:05:30.910 --> 00:05:33.610
we don't think there's any problems. It is a clean exit.

90
00:05:33.970 --> 00:05:38.490
Anything in non-zero exit would be considered something was went wrong. Um,

91
00:05:38.670 --> 00:05:41.450
and also, you know, you don't have to use exit one,

92
00:05:41.450 --> 00:05:43.370
you could use an exit five but then you know,

93
00:05:43.470 --> 00:05:48.210
you would have to then have a way to map what those exit codes mean to

94
00:05:48.210 --> 00:05:52.770
some sort of missing file or some other type of error base. But okay,

95
00:05:53.030 --> 00:05:56.410
so we have an exit one here on line seven and if we go to line nine here we have

96
00:05:56.410 --> 00:05:57.243
another echo.

97
00:05:57.660 --> 00:06:01.120
And then this echo you can see it says this will not be printed and the reason

98
00:06:01.140 --> 00:06:05.080
why it won't is because line seven is going to exit out the,

99
00:06:05.260 --> 00:06:06.920
so line nine will never run.

100
00:06:07.220 --> 00:06:09.600
So let's go ahead and we're gonna get out of this file.

101
00:06:09.840 --> 00:06:13.000
I just wanted to show you comments how to print to the screen and how to get out

102
00:06:13.000 --> 00:06:15.440
of a script early or to um,

103
00:06:15.800 --> 00:06:18.600
announce that it was a good exit with an exit zero.

104
00:06:19.620 --> 00:06:23.960
So in them I do a shift colon, um, shift colon queue,

105
00:06:24.210 --> 00:06:28.480
which will go ahead and quit and get us outta here. Um,

106
00:06:29.100 --> 00:06:33.160
and so let's go ahead and run this. Um, if you've clone this repository,

107
00:06:33.700 --> 00:06:36.960
all the files are already executable so we don't need to do anything.

108
00:06:37.220 --> 00:06:40.440
But if for some reason it doesn't execute, use the chima cam,

109
00:06:40.580 --> 00:06:45.400
use the chaud command and you could do this if you get a

110
00:06:45.400 --> 00:06:46.640
permission deny trying to run it,

111
00:06:46.640 --> 00:06:49.320
you could do a chaud plus X which will make it executable.

112
00:06:49.560 --> 00:06:53.400
I could do it and it's item potent, it won't hurt it. So, um,

113
00:06:53.780 --> 00:06:57.200
but we'll get rid of this and we're gonna run this command right from this

114
00:06:57.200 --> 00:07:01.400
directory in section five. And as you see we only got hello everyone.

115
00:07:01.400 --> 00:07:06.400
We didn't get the last line that says this line will not be printed and in bash

116
00:07:06.770 --> 00:07:11.480
there is a built in variable that

117
00:07:11.540 --> 00:07:16.440
allows us to see what was the last exit code of the last command we

118
00:07:16.440 --> 00:07:16.880
just ran.

119
00:07:16.880 --> 00:07:21.840
And in this case we ran the dummy script and as you could see when I echo

120
00:07:22.600 --> 00:07:25.320
whatever was in that variable right there, that special,

121
00:07:25.510 --> 00:07:27.560
that special variable with the doll sign. Question mark,

122
00:07:27.740 --> 00:07:32.120
you can see we have the one which is exactly what we saw in that following line

123
00:07:32.120 --> 00:07:35.220
seven was exit one. Okay,

124
00:07:35.720 --> 00:07:37.660
so I'm gonna go ahead and clear the screen one more time.

125
00:07:38.780 --> 00:07:40.080
I'm gonna move on to variables.

126
00:07:41.790 --> 00:07:46.030
Variables are aliases that can hold information about something else. Um,

127
00:07:46.390 --> 00:07:49.830
I think this will do a lot better once we open it up and look at some examples.

128
00:07:51.210 --> 00:07:53.270
So let's go ahead. I'm gonna do vim

129
00:07:54.770 --> 00:07:58.770
section five and then we're gonna look for the variables file.

130
00:07:59.230 --> 00:08:01.530
So Vim, hands-on Lennox for DevOps,

131
00:08:01.620 --> 00:08:06.170
cloud engineers directory section five sub directory and the variables SH five.

132
00:08:07.830 --> 00:08:11.770
Okay, let's open this up. Okay,

133
00:08:12.310 --> 00:08:15.650
so line number one, starting to look familiar.

134
00:08:15.650 --> 00:08:18.450
Hopefully the shebang, we need it every time.

135
00:08:19.160 --> 00:08:20.810
Line two just has an empty comment.

136
00:08:21.910 --> 00:08:25.810
So lines three through five are variables and you could tell they're variables

137
00:08:25.810 --> 00:08:29.890
because you have uh, whatever it is in this case. Line three is CPUs.

138
00:08:29.890 --> 00:08:33.610
You have an equal signs and a one and you notice there's no spaces or anything

139
00:08:33.610 --> 00:08:38.100
between them. And here we say CPUs equals one line four,

140
00:08:38.100 --> 00:08:41.300
we say memory equals 1,024. And then use a name.

141
00:08:41.320 --> 00:08:45.300
You could see that we use double quotes to denote that this is a string that we

142
00:08:45.300 --> 00:08:49.180
want to represent and it's better to be explicit than implicit. Here.

143
00:08:49.490 --> 00:08:54.080
It's okay to uh, call out your types that way. And as you can tell here,

144
00:08:54.080 --> 00:08:56.900
the variables are lowercase. You may run,

145
00:08:57.000 --> 00:09:00.380
you may run into variables and other systems that are uppercase,

146
00:09:00.920 --> 00:09:05.020
but Bash itself uses environment variables that are all uppercase

147
00:09:05.840 --> 00:09:10.840
and to avoid accidentally running into, uh,

148
00:09:10.840 --> 00:09:14.880
confusing yourself and other people of which is the system variable and which is

149
00:09:14.890 --> 00:09:17.800
yours. If it's a local script, these variables are local,

150
00:09:18.220 --> 00:09:21.160
go ahead and you should keep them lowercase. Just be consistent.

151
00:09:21.340 --> 00:09:23.480
That's all that really matters here. Okay,

152
00:09:24.060 --> 00:09:28.280
so we have these three variables and we go down the line seven and eight and

153
00:09:28.280 --> 00:09:30.040
then this is us just printing them out.

154
00:09:31.100 --> 00:09:34.640
The one thing that's worth pointing out here is I want you to show here is this

155
00:09:34.820 --> 00:09:38.920
dollar sign, curly braces inside the double quote string.

156
00:09:39.260 --> 00:09:44.240
And what is special about this in bash is whatever is inside these double curly

157
00:09:44.260 --> 00:09:46.160
braces, it means to expand.

158
00:09:46.540 --> 00:09:50.940
And so it basically takes whatever the variable is in this case CPUs,

159
00:09:50.990 --> 00:09:55.980
which is one, it'll just expand this variable and replace this whole

160
00:09:57.120 --> 00:10:00.410
placeholder here with the, with the number one inside this string.

161
00:10:00.880 --> 00:10:04.230
Exactly the same thing for memory and username. Alright,

162
00:10:04.610 --> 00:10:05.830
so let's go ahead and close this.

163
00:10:05.830 --> 00:10:09.190
Let's run it and we obviously have a good idea of what it should print up.

164
00:10:09.190 --> 00:10:11.950
Let's go ahead and do it anyway. So shift colon Q.

165
00:10:12.940 --> 00:10:14.280
So we'll quit out of this file.

166
00:10:15.670 --> 00:10:20.330
I'm gonna hit the up arrow and then I'm gonna hit control A to go to the front

167
00:10:20.330 --> 00:10:25.190
of the line and then I'm just gonna delete the vim command and then I'll run

168
00:10:25.190 --> 00:10:26.010
this file.

169
00:10:26.010 --> 00:10:30.270
And you can see this host has one CPU and 1,024

170
00:10:30.510 --> 00:10:33.310
kilobytes of memory. The active user is EC two users.

171
00:10:33.410 --> 00:10:37.030
So you could see how we can grab variables, set them,

172
00:10:37.090 --> 00:10:41.950
and then display them out with the echo command to the rest of the system

173
00:10:42.010 --> 00:10:45.260
or to the screen I should say. Okay,

174
00:10:46.050 --> 00:10:49.510
so let's move on. I'm gonna clear the screen and then we're gonna move on to it.

175
00:10:49.530 --> 00:10:51.590
Uh, one of the, the data types,

176
00:10:51.610 --> 00:10:54.910
the most main data type you're gonna use in Bash and it's arrays.

177
00:10:55.330 --> 00:10:57.590
So there are two types of arrays you can use in Bash.

178
00:10:58.430 --> 00:11:02.210
The first one is a dimensional indexed array and the other is an associative

179
00:11:02.210 --> 00:11:03.043
array.

180
00:11:03.070 --> 00:11:07.490
So a regular indexed array is like probably one you're used to seeing in other

181
00:11:07.640 --> 00:11:09.170
program language. Um,

182
00:11:09.170 --> 00:11:13.410
you can have a list of something but you can access it by using an integer based

183
00:11:13.500 --> 00:11:17.330
index, you know, 0 1, 2 3 and it'll give you the element at that space.

184
00:11:17.390 --> 00:11:20.400
In the array associative arrays,

185
00:11:20.580 --> 00:11:23.920
you string indexes like keys so you can reference the elements of the arrays,

186
00:11:23.920 --> 00:11:28.760
like almost like a hash or a map except it's still an array and you use

187
00:11:28.780 --> 00:11:31.680
the string as the key instead of a positive be integer. Okay,

188
00:11:31.820 --> 00:11:36.800
so let's go ahead and we're gonna open up the arrays file and that

189
00:11:36.800 --> 00:11:40.780
is in section five arrays.sh.

190
00:11:43.020 --> 00:11:46.870
Okay, so line one, our shebang. Um, I'm,

191
00:11:46.890 --> 00:11:49.270
I'm not gonna keep saying that over and over again,

192
00:11:49.270 --> 00:11:53.520
but I just want to drill in that the shebang line is extremely important.

193
00:11:56.970 --> 00:11:59.540
Line three, you can see we have a declare keyword.

194
00:12:00.080 --> 00:12:04.700
So the declare keyword is a bash keyword which allows us to declare array.

195
00:12:05.360 --> 00:12:07.900
And as you can see here, we're using a

196
00:12:09.430 --> 00:12:11.300
minus small A, right?

197
00:12:11.320 --> 00:12:15.300
So that's for the index based the integer index based array.

198
00:12:15.640 --> 00:12:20.380
And down here on line 16 use the capital dash A for the associative

199
00:12:20.400 --> 00:12:21.540
arrays. Okay,

200
00:12:22.150 --> 00:12:26.730
so up here we're gonna call this array distros and this is just Amazon Lennox,

201
00:12:26.850 --> 00:12:31.640
I mean sorry, this is just Lennox Distros. And so lines four through eight,

202
00:12:32.040 --> 00:12:33.960
I am just setting it. For example,

203
00:12:34.620 --> 00:12:39.510
at index zero we're using Amazon Lennox at index

204
00:12:39.530 --> 00:12:42.110
one we have a Buntu index, two red hat,

205
00:12:42.250 --> 00:12:45.030
so on and so on up to index five where it's arch.

206
00:12:46.590 --> 00:12:49.610
So line 10 is where we're actually printing it out,

207
00:12:49.670 --> 00:12:53.810
but we're using this special built in character,

208
00:12:53.810 --> 00:12:56.730
the at symbol that deals with bash arrays.

209
00:12:56.730 --> 00:13:00.450
And what this tells bash to do is is it explode the array on one line.

210
00:13:01.070 --> 00:13:05.890
So it basically says go ahead and put all the elements of

211
00:13:06.150 --> 00:13:09.850
of this array on one line eliminated by space.

212
00:13:10.670 --> 00:13:15.490
So as you can see here we have the dollar sign curly braces

213
00:13:15.490 --> 00:13:15.840
again,

214
00:13:15.840 --> 00:13:20.570
meaning whatever's inside the curly braces explode out and then

215
00:13:20.570 --> 00:13:24.770
it'll dump it in the strings. Now line 11 is interesting.

216
00:13:24.830 --> 00:13:28.690
So let's say that you did your raise but you realize that oh

217
00:13:30.140 --> 00:13:34.450
Devin should not have been included in this list for for whatever reason you can

218
00:13:34.450 --> 00:13:39.450
use the unset command and you pass it the index and the array that you want and

219
00:13:39.450 --> 00:13:43.770
it'll take the Debian version out. So we could see on line 10 here,

220
00:13:43.780 --> 00:13:47.450
we're printing out all of them, which should be Amazon Lennox through arch.

221
00:13:48.210 --> 00:13:52.270
We take Debbie and out and then we print it again and we should get the array

222
00:13:52.440 --> 00:13:53.430
minus Debian.

223
00:13:53.810 --> 00:13:57.790
And this right here is just a simple line separator that I've put in there just

224
00:13:57.790 --> 00:14:00.910
so you can see the difference between the both print statements of the array,

225
00:14:00.910 --> 00:14:05.150
the two echo statements, sorry. Okay, so let's go down to line 16.

226
00:14:05.150 --> 00:14:08.430
This is where we do the associative array and we're gonna call an associative

227
00:14:08.430 --> 00:14:13.120
array just command and line 17 and 18 is where we're

228
00:14:13.360 --> 00:14:16.480
actually setting the array. Uh, the command's array.

229
00:14:16.700 --> 00:14:18.600
And you could see instead of using zero,

230
00:14:18.930 --> 00:14:21.640
we're actually using the command name in this case would be Ls.

231
00:14:22.220 --> 00:14:27.200
And then the value which will be in the array is where it is located

232
00:14:27.300 --> 00:14:28.400
on in the directory.

233
00:14:29.140 --> 00:14:33.720
So you could say down here on line 18 we're saying the cat command is located in

234
00:14:33.720 --> 00:14:36.520
the bin directory. And so all those,

235
00:14:37.630 --> 00:14:42.280
this is a way if you are not using the positive integers and sometimes you know

236
00:14:42.280 --> 00:14:45.640
that might be a little bit harder to understand or order or uh,

237
00:14:45.710 --> 00:14:50.520
quicker lookup means something to you or you need to be able to call it by a

238
00:14:50.520 --> 00:14:54.430
name. This is a good, you can use an associative array. So down here on line 20,

239
00:14:54.760 --> 00:14:59.620
we're just showing how you can reach into that array

240
00:14:59.720 --> 00:15:04.140
in between the dollas sign curly braces to expand it and just use CAT as the

241
00:15:04.190 --> 00:15:05.380
index. All right,

242
00:15:05.960 --> 00:15:09.260
so let's run this and let's see everything that we just learned about arrays out

243
00:15:09.260 --> 00:15:13.660
on the screen. So shift colon q quit this file.

244
00:15:15.360 --> 00:15:16.520
I will clear the screen,

245
00:15:17.960 --> 00:15:22.740
put the up arrow to get to our command control A to go to the beginning of the

246
00:15:22.740 --> 00:15:26.600
line, get rid of the vem command and hit return.

247
00:15:26.940 --> 00:15:31.920
And so you can see here this was the first echo command where we exploded

248
00:15:31.920 --> 00:15:35.640
the whole array out and we printed out each of the elements and there's five uh,

249
00:15:35.700 --> 00:15:36.533
six there.

250
00:15:37.340 --> 00:15:41.490
There was the line separator and this was after we unset debian.

251
00:15:41.870 --> 00:15:44.130
So if you look down here, there's only five in this one,

252
00:15:44.150 --> 00:15:46.890
Debian is gone from this line. Okay?

253
00:15:47.470 --> 00:15:51.730
And here you could see where we'd use the associative array where we said the

254
00:15:51.730 --> 00:15:56.210
CAT command is located in the bin director and we use CAT as the index. Alright,

255
00:15:56.920 --> 00:16:00.570
that is it for arrays for now. So let me go ahead and clear the screen.

256
00:16:01.190 --> 00:16:03.730
And so the next thing that we're gonna get into is branching.

257
00:16:03.910 --> 00:16:07.810
So we need some sort of logic base when when we're talking and moving around the

258
00:16:07.810 --> 00:16:08.190
code,

259
00:16:08.190 --> 00:16:12.850
we need to be able to check values of things and do something if a certain

260
00:16:13.170 --> 00:16:17.330
criteria is met. And so we'll start out with if so, the if statement.

261
00:16:17.710 --> 00:16:21.090
So the if command is a branching command that you can use to make logic

262
00:16:21.450 --> 00:16:24.570
decisions. So like if this, then do that.

263
00:16:24.750 --> 00:16:27.650
Or you can have multiple branch strategies, you have multiple if statements,

264
00:16:27.880 --> 00:16:31.290
nest if statements like if this thing do this,

265
00:16:31.920 --> 00:16:33.530
otherwise do this other thing.

266
00:16:34.070 --> 00:16:38.880
So let's go ahead and let's look at the if file and it is

267
00:16:38.880 --> 00:16:43.830
under section five again and it is called if sh I'm

268
00:16:43.830 --> 00:16:48.240
opening it with the vem command once again. Okay,

269
00:16:48.580 --> 00:16:53.000
so as you can see on line three we create a variable called CPU and we

270
00:16:53.180 --> 00:16:54.600
assign it a value of six.

271
00:16:56.300 --> 00:16:58.240
And if we go down here to line six,

272
00:16:59.020 --> 00:17:02.400
the bash if statement uses the keyword if,

273
00:17:03.640 --> 00:17:05.820
and you can notice these single brackets right here.

274
00:17:06.080 --> 00:17:10.340
So these single brackets are also built into bash and what they mean is they

275
00:17:10.340 --> 00:17:11.173
mean test.

276
00:17:11.360 --> 00:17:16.220
So they're basically saying test whatever this expression is inside

277
00:17:16.710 --> 00:17:20.900
these brackets, test that and if it's true then do this first thing,

278
00:17:20.970 --> 00:17:24.700
then do line eight. If you notice that if it's false,

279
00:17:25.110 --> 00:17:30.100
there is nothing else you can do on that line that it just won't print it out.

280
00:17:30.160 --> 00:17:32.860
And in this case it's just printing out it's suitable virtualization.

281
00:17:33.000 --> 00:17:37.980
So if the variable CPU and you notice that we have to reference

282
00:17:38.040 --> 00:17:39.740
the CPU variable with a dollar sign.

283
00:17:39.740 --> 00:17:43.220
So Bash knows that we mean the variable CPU and not just a string.

284
00:17:44.100 --> 00:17:48.600
We use a dash gt which is greater than there's obviously dash,

285
00:17:48.600 --> 00:17:52.650
less than dash greater than or equal to dash,

286
00:17:52.650 --> 00:17:56.330
less than equal to or just uh, equal or not equal.

287
00:17:57.230 --> 00:17:58.370
And those are used,

288
00:17:58.380 --> 00:18:02.690
these dash ones and why you want to use those instead of the other operators

289
00:18:02.690 --> 00:18:03.610
that you think of like a,

290
00:18:03.730 --> 00:18:08.410
a right hand greater than arrow and other things is that Bash uses these for um,

291
00:18:08.410 --> 00:18:12.290
numeric comparisons. And so it's good to just stick with the uh,

292
00:18:12.390 --> 00:18:15.290
the paradigm that Bash expects. The other way is fine too.

293
00:18:15.760 --> 00:18:19.730
This is probably more portable on bash I believe so it's probably worth using

294
00:18:19.730 --> 00:18:20.550
it. But anyway,

295
00:18:20.550 --> 00:18:24.450
we could see here we're saying if C P U count or whatever C P U is,

296
00:18:24.450 --> 00:18:29.350
if it's greater than four, then print this line out. So line 11,

297
00:18:29.350 --> 00:18:31.390
just an empty echo, I'm just making some space here.

298
00:18:31.390 --> 00:18:33.350
So let's go on to something a little bit more interesting here.

299
00:18:33.810 --> 00:18:37.430
So on line 13 through 18, which I have highlighted right here,

300
00:18:38.210 --> 00:18:42.350
I'm saying if the CPU is greater than four, which is the same line of six,

301
00:18:42.860 --> 00:18:44.870
then echos suitable virtualization.

302
00:18:44.870 --> 00:18:49.830
But here we have the else command on line 16 and we're saying if it's not

303
00:18:49.830 --> 00:18:53.350
suitable, so if this test right here is CPU greater than four,

304
00:18:53.730 --> 00:18:57.670
if it evaluates false, then we run line 17.

305
00:18:57.930 --> 00:19:01.030
And of course we just echo out a silly uh phrase here.

306
00:19:02.230 --> 00:19:07.190
So inside these bracket tests and we'll jump down

307
00:19:07.190 --> 00:19:08.023
the line 21,

308
00:19:09.140 --> 00:19:14.120
this dash F is a built in test inside these brackets for

309
00:19:14.120 --> 00:19:17.480
dash. And what this allows us to do, it says does this file exist?

310
00:19:18.310 --> 00:19:22.600
There's a dash D for does a directory exist? There's a dash z um,

311
00:19:22.600 --> 00:19:25.560
to see if an argument is populated. There's a whole bunch of 'em,

312
00:19:25.560 --> 00:19:26.640
I won't go with 'em all over here.

313
00:19:26.980 --> 00:19:30.400
You can find on using the demand page or you can also Google them of course,

314
00:19:31.120 --> 00:19:34.900
but these are super handy if you wanna test for edge cases or corner cases of

315
00:19:34.900 --> 00:19:37.580
something being there or not there instead of having to write your own.

316
00:19:38.080 --> 00:19:41.380
So basically what this if block does from lines 21 through 26,

317
00:19:41.380 --> 00:19:45.860
it says if the Aira file exists then echo file exists,

318
00:19:46.370 --> 00:19:50.670
otherwise echo the file does not exist. Okay,

319
00:19:50.880 --> 00:19:53.030
let's go ahead and close this file,

320
00:19:53.030 --> 00:19:57.820
which is shift colon Q and we're gonna run it. So I'm gonna clear the screen,

321
00:19:58.090 --> 00:20:01.780
I'll hit the up arrow once again, control A to the front of the line,

322
00:20:03.050 --> 00:20:07.510
run it and you could see the first if block was suitable for ation because six

323
00:20:07.530 --> 00:20:11.160
was greater than four, you could see the second one,

324
00:20:11.550 --> 00:20:15.080
even though we had the Ls, was still suitable for virtualization.

325
00:20:15.180 --> 00:20:18.560
But if for some reason we changed that CPU variable to say three,

326
00:20:18.560 --> 00:20:23.000
which is less than four, then we would've got this line would've never printed.

327
00:20:23.340 --> 00:20:25.640
But this line would've said your mileage may vary.

328
00:20:26.220 --> 00:20:29.680
And here we tested with the dash F builtin to see if the file existed.

329
00:20:31.710 --> 00:20:36.240
Okay, so that is it for IFS and if else's, let me clear the screen here.

330
00:20:37.530 --> 00:20:39.790
So the next thing that we're gonna go over is loops.

331
00:20:40.010 --> 00:20:44.070
So loops are great when you need to do something over and over again. Um,

332
00:20:45.130 --> 00:20:46.150
and you need to uh,

333
00:20:46.330 --> 00:20:50.880
do something to maybe a variable or a file

334
00:20:51.380 --> 00:20:54.360
or go fetch a bunch of things and something that's repetitive.

335
00:20:54.780 --> 00:20:56.400
So there's two loops you can use in bash.

336
00:20:56.620 --> 00:21:00.600
One is the four loop with the four key word f o r and the other one is the wild

337
00:21:00.670 --> 00:21:03.080
loop and we're gonna see both of them in its file.

338
00:21:03.380 --> 00:21:04.840
So let's go ahead and use Vim again.

339
00:21:08.180 --> 00:21:11.800
We'll go section five and we're gonna say loops.

340
00:21:13.150 --> 00:21:15.960
Okay, so there is a lot going on here. Um,

341
00:21:16.100 --> 00:21:20.080
so what we'll try to do is just go through the parts that that are new to us

342
00:21:20.080 --> 00:21:24.310
instead of the old. So if we scroll down, um,

343
00:21:24.310 --> 00:21:25.230
you can use in vim,

344
00:21:25.230 --> 00:21:29.950
you can use the arrow keys down or you can use the J command and the J command

345
00:21:29.950 --> 00:21:32.770
will allow you to go one line down at a time. Okay,

346
00:21:32.870 --> 00:21:36.050
so let's go down to line 12 and you can see so uh,

347
00:21:36.300 --> 00:21:40.010
lines three through eight we're just creating the distros again an array that

348
00:21:40.010 --> 00:21:42.170
which you saw the rays have followed. Nothing different there.

349
00:21:42.390 --> 00:21:45.850
And the reason why we're just using that so we can use the four loops and the

350
00:21:45.850 --> 00:21:50.320
wilds to print over stuff right here. So lines 12 to 15,

351
00:21:50.500 --> 00:21:52.080
we have four loop,

352
00:21:52.130 --> 00:21:56.680
which is with using the four key word and distro rate here is just a

353
00:21:56.920 --> 00:22:00.120
temporary variable placeholder. And you could see here that we're,

354
00:22:00.120 --> 00:22:04.000
we're doing the dollar sign curly braces of the distro with the at symbol

355
00:22:04.000 --> 00:22:09.000
meaning expand all the members of our array onto one line and then

356
00:22:09.000 --> 00:22:11.600
let's loop over 'em. And so for each pass of this loop,

357
00:22:11.820 --> 00:22:16.560
distro will become one of these and there's a weird,

358
00:22:16.780 --> 00:22:21.600
um, not a weird, but there is a small inconsistency here. When we run this,

359
00:22:21.600 --> 00:22:23.560
you'll see it. Let me know, uh, pause the video,

360
00:22:23.560 --> 00:22:26.600
take a look at the code and tell me if you can spot it. And then we run it.

361
00:22:26.630 --> 00:22:30.550
I'll explain it. So basically for each of the loops,

362
00:22:30.660 --> 00:22:34.630
each iteration of the loop, we do in this case just line 14,

363
00:22:34.690 --> 00:22:39.150
we just echo whatever distro variables holding at the time and then we use the

364
00:22:39.150 --> 00:22:42.990
done to go ahead and be done with that block. Okay, so that's the four loop

365
00:22:44.610 --> 00:22:46.900
line 17, just the line separated.

366
00:22:46.900 --> 00:22:49.100
So you can see the differences between each four loops.

367
00:22:49.480 --> 00:22:52.100
So line 19 through 27,

368
00:22:52.200 --> 00:22:56.380
or sorry 20 through 27 is another four loop over the Lennox distros.

369
00:22:57.210 --> 00:23:00.190
But this time we're using a keyword called break.

370
00:23:00.530 --> 00:23:05.350
And so basically what we're saying on lines 22 is we're saying if the distro and

371
00:23:05.350 --> 00:23:08.550
you notice that we're using the dollar sign and we're putting the variable in

372
00:23:08.550 --> 00:23:10.790
quotes here because we're comparing strings.

373
00:23:11.050 --> 00:23:13.670
So both sides need to be strings and bash when comparing them.

374
00:23:13.840 --> 00:23:16.510
We're saying if distro equals red hat then break.

375
00:23:16.850 --> 00:23:20.630
And what break does is going to stop this loop completely.

376
00:23:20.630 --> 00:23:22.430
You're gonna jump right out of the for loop.

377
00:23:23.140 --> 00:23:25.400
And as you can see what will happen is you'll get,

378
00:23:25.660 --> 00:23:29.120
you'll print out the first indices, the second INDs,

379
00:23:29.180 --> 00:23:33.160
you'll get to the third industry indices and you'll see line 22 will say,

380
00:23:33.500 --> 00:23:36.080
Hey is if distro equals red hat, then break.

381
00:23:36.500 --> 00:23:40.200
So you're only gonna end up seeing this printed out and we'll see that when we

382
00:23:40.200 --> 00:23:42.320
run it. All right,

383
00:23:42.660 --> 00:23:45.280
so let's jump down to line 29.

384
00:23:46.630 --> 00:23:48.770
And in them if you want to go to line numbers,

385
00:23:48.790 --> 00:23:51.650
you can do a shift colon and then the line number like it did here,

386
00:23:51.650 --> 00:23:53.290
which I just did shift colon 29.

387
00:23:55.110 --> 00:23:59.970
So this is a wild loop and wild loops keep doing iterations in a loop until

388
00:24:00.090 --> 00:24:03.610
a condition is met. And in this case it's inside these brackets.

389
00:24:03.770 --> 00:24:04.650
Remember this is a test.

390
00:24:05.470 --> 00:24:10.040
So test says variable count is less than 10

391
00:24:10.730 --> 00:24:14.630
and you can see right now count on line 31 variable is set to zero.

392
00:24:15.250 --> 00:24:19.630
So this loop is going to actually start because it is less than 10.

393
00:24:20.470 --> 00:24:24.970
And then here what we do on each iteration loop is we echo count

394
00:24:25.650 --> 00:24:28.490
whatever count is then line 35,

395
00:24:28.520 --> 00:24:32.330
this is a special operator and you can see it's the double parenthesis.

396
00:24:32.910 --> 00:24:35.850
And then inside the double parenthesis what that tells bash is that this is a

397
00:24:35.850 --> 00:24:39.860
mathematical expression. And here we can do count plus plus.

398
00:24:40.280 --> 00:24:43.980
And so a count plus plus means it just means increment count by one.

399
00:24:45.010 --> 00:24:47.270
So once this will keep going iteration,

400
00:24:47.270 --> 00:24:50.630
the first loop iteration count will be one second iteration count will be two,

401
00:24:51.040 --> 00:24:55.710
third iteration count will be three, so on and so on until count hits 10, right?

402
00:24:55.820 --> 00:24:57.990
Then count is not less than 10.

403
00:24:58.250 --> 00:25:02.710
So then this wild loop stops all on its own and then it proceeds to the next

404
00:25:02.710 --> 00:25:06.550
line in this case would be line 38, which is echo countdown.

405
00:25:07.490 --> 00:25:09.910
All right, so let's go down to line 40 here. So you can see,

406
00:25:09.910 --> 00:25:13.110
so this is the last while loop. And so this one is just a little bit different.

407
00:25:13.110 --> 00:25:14.230
Instead of counting down,

408
00:25:14.280 --> 00:25:17.430
we're gonna count backwards like you would on a launch.

409
00:25:18.370 --> 00:25:21.430
And so here you can see count to set the 10.

410
00:25:21.490 --> 00:25:25.710
Now the variable and then we say when count is greater than zero,

411
00:25:26.440 --> 00:25:30.480
it'll actually circumvent this, this loop.

412
00:25:30.900 --> 00:25:33.000
And right now it's 10 so it would just keep going.

413
00:25:33.140 --> 00:25:36.080
And we do the same thing we did in the other loop. We echo the count,

414
00:25:36.680 --> 00:25:38.140
we do a mathematical operation,

415
00:25:38.140 --> 00:25:43.020
but this time we do a minus minus which means to decrement to take one away from

416
00:25:43.020 --> 00:25:46.060
count. Each iteration here on line 45,

417
00:25:46.100 --> 00:25:50.220
I just put a little small sleep of 200 milliseconds and that is just to give the

418
00:25:50.220 --> 00:25:53.380
effect of a little bit of pause as it counts down to give it like that,

419
00:25:53.610 --> 00:25:56.400
that timer aspect. And then you,

420
00:25:56.850 --> 00:25:59.200
while once that timer reaches zero,

421
00:25:59.830 --> 00:26:02.160
then this whole wild loop will end,

422
00:26:02.490 --> 00:26:05.920
it'll jump back out of the wild loop and then you'll end up with four, uh,

423
00:26:05.920 --> 00:26:10.510
line 48 which is just printing out to blasto. Okay, that was a lot.

424
00:26:10.770 --> 00:26:14.470
So let's go ahead and run it and let's see it happen. So shift colon Q.

425
00:26:16.610 --> 00:26:16.950
All right,

426
00:26:16.950 --> 00:26:21.550
we're gonna hit the up arrow control A and then let's go ahead and run the loop.

427
00:26:24.370 --> 00:26:24.960
Okay,

428
00:26:24.960 --> 00:26:29.580
so you saw we printed out the array first Amazon

429
00:26:29.700 --> 00:26:34.340
Lennox TU arch here we printed out Amazon,

430
00:26:34.460 --> 00:26:38.740
Lennox a Buntu and then we have this count up we counted from zero to nine

431
00:26:39.320 --> 00:26:42.980
and then we did a countdown where we counted from 10 to one and you could saw

432
00:26:43.000 --> 00:26:46.670
that it paused between and then we blasted. Okay,

433
00:26:46.890 --> 00:26:51.070
so that is it for loops. Let's move on to the next paradigm.

434
00:26:51.370 --> 00:26:53.230
And this is going to be switches.

435
00:26:53.610 --> 00:26:57.510
So sometimes the if else logic like you saw can get complicated,

436
00:26:57.690 --> 00:27:00.110
it can be error prone and sometimes hard to read.

437
00:27:00.330 --> 00:27:03.430
So BAS gives us another flow pattern called switches.

438
00:27:03.430 --> 00:27:07.800
So let's go ahead and open up the file for switches. It's in section five

439
00:27:10.710 --> 00:27:15.030
switch.sh. Okay,

440
00:27:15.890 --> 00:27:18.110
so here we're doing something a little bit different.

441
00:27:19.420 --> 00:27:21.520
So line three here we print out,

442
00:27:21.620 --> 00:27:26.480
we say enter a person's name and now we're using this new bash function

443
00:27:26.740 --> 00:27:29.080
or bash command keyword called read.

444
00:27:29.340 --> 00:27:33.520
And what this does is it takes something from standard in and in this case it'll

445
00:27:33.520 --> 00:27:37.760
prompt, it'll block waiting for you to type something into the terminal. Um,

446
00:27:37.760 --> 00:27:41.760
and in this case it's asking for a name, we're asking for a person's name.

447
00:27:42.060 --> 00:27:44.120
And then what happens here on line four,

448
00:27:44.680 --> 00:27:47.800
whatever it reads in from the terminal with a return statement,

449
00:27:47.800 --> 00:27:49.200
once a return is hit,

450
00:27:49.570 --> 00:27:54.000
it'll go ahead and apply whatever we typed in into this user variable. Uh,

451
00:27:54.000 --> 00:27:58.560
if you come outta the line six here we use a switch statement and here it says

452
00:27:58.630 --> 00:27:59.220
case.

453
00:27:59.220 --> 00:28:04.200
And so what case statement basically does we case on whatever the

454
00:28:04.200 --> 00:28:08.670
value of user ends up being And then the rest of this

455
00:28:09.250 --> 00:28:11.670
syntax, while it looks maybe a little bit interesting,

456
00:28:12.430 --> 00:28:15.570
you could see here that we use the EAC to close the case,

457
00:28:15.570 --> 00:28:18.090
which is case backwards. And if you notice if has fi,

458
00:28:18.480 --> 00:28:21.770
it's the same thing and this alerts bashed to know that we're done with this

459
00:28:21.770 --> 00:28:25.850
block. But so in the case statement, whatever this value is,

460
00:28:25.950 --> 00:28:29.730
if it matches a Kira, then we echo this line,

461
00:28:29.790 --> 00:28:33.750
we echo line eight and these just mean to go to the next one.

462
00:28:33.750 --> 00:28:36.870
These are just separated as ca switch has a specific format.

463
00:28:36.930 --> 00:28:40.840
So you gotta follow it uh, to the letter here. If it doesn't match a Kira,

464
00:28:40.840 --> 00:28:43.600
it'll then hit this one, it goes top down. Okay?

465
00:28:44.810 --> 00:28:47.670
If the user is Leilani, we just, we echo uh,

466
00:28:47.670 --> 00:28:52.380
sentiment here and then the star one right here is basically a default and

467
00:28:52.380 --> 00:28:52.960
catchall.

468
00:28:52.960 --> 00:28:56.700
And this is a very good habit to get in because if it didn't match any of these,

469
00:28:56.760 --> 00:28:59.140
it would just end out out of this and nothing would happen.

470
00:28:59.400 --> 00:29:02.780
You would jump outta line 16 and there was nothing else in the script.

471
00:29:02.780 --> 00:29:06.020
So the script would just end and nothing would ever be printed on screen except

472
00:29:06.320 --> 00:29:10.560
the lines three and four or lines three. So what this basically says,

473
00:29:10.560 --> 00:29:14.150
well if you don't match any of these things that I have in here,

474
00:29:14.150 --> 00:29:15.630
obviously you can have as many as you want.

475
00:29:15.710 --> 00:29:20.350
I only put two in here just for example. But basically then you print out like,

476
00:29:20.350 --> 00:29:23.640
I have no idea. All right, so that is our switch statement. That's good.

477
00:29:23.640 --> 00:29:27.440
It cleans up your if logic and it makes it a little bit easier to read and you

478
00:29:27.440 --> 00:29:31.860
can do matching from top down. All right, so shift colon Q,

479
00:29:32.470 --> 00:29:35.540
we'll put this file up arrow control A,

480
00:29:36.700 --> 00:29:40.220
let's run the switch command. So as you see here,

481
00:29:40.240 --> 00:29:43.640
it asks us the enter the person's name and the reed command is blocking now,

482
00:29:43.640 --> 00:29:48.120
which is waiting for us to type something. So I will type the name Akira,

483
00:29:48.300 --> 00:29:51.960
hit return and it says I do not know them that way, right?

484
00:29:52.980 --> 00:29:57.610
So we run it again and let's say I say Bob, we we get the,

485
00:29:57.730 --> 00:30:01.410
I have no idea who Bob is and that is the catchall that we were talking about,

486
00:30:01.410 --> 00:30:03.570
the asterisk one. Alright, lemme clear the screen.

487
00:30:06.440 --> 00:30:09.020
So the last file we're gonna go over is called function.

488
00:30:09.120 --> 00:30:13.220
So functions allow you to encapsulate behavior into an isolated call.

489
00:30:13.720 --> 00:30:17.500
It makes your code easier to read and you can put light things together so it's

490
00:30:17.500 --> 00:30:20.100
easier for people who come up behind you look a descript a reason about it.

491
00:30:20.760 --> 00:30:21.700
So let's go ahead

492
00:30:23.550 --> 00:30:28.350
and open up our section five and with safe

493
00:30:28.670 --> 00:30:32.210
function, okay,

494
00:30:32.470 --> 00:30:37.360
so functions like on line five and live line nine and

495
00:30:37.360 --> 00:30:41.960
line 17 to begin with a name, open print,

496
00:30:41.960 --> 00:30:46.200
close print. And here we're using underscores here it's more of a convention.

497
00:30:46.200 --> 00:30:48.080
You can use whatever you want, but this is what I,

498
00:30:48.620 --> 00:30:52.280
and whatever's in the function is encapsulated here by curly braces.

499
00:30:53.270 --> 00:30:58.210
So the function on line five basically is going to echo the

500
00:30:58.210 --> 00:31:03.050
host name and you can see how this variable with the doll sign all caps.

501
00:31:03.520 --> 00:31:08.050
That is because that is a bash host provided environment variable,

502
00:31:08.390 --> 00:31:09.450
not one that we created.

503
00:31:09.910 --> 00:31:14.750
And it'll just give us the host name of our EC two ones here on line

504
00:31:14.860 --> 00:31:18.190
nine. We create a home get home function.

505
00:31:18.770 --> 00:31:22.150
And you notice how these functions have names that describe what they do with

506
00:31:22.150 --> 00:31:23.990
that's, it's just a really good um,

507
00:31:24.840 --> 00:31:28.700
way to think about how you wanna write functions instead of making it very

508
00:31:28.700 --> 00:31:29.140
simplistic,

509
00:31:29.140 --> 00:31:33.100
it it'd be hard a year from now if you go back and look at this may be hard if

510
00:31:33.100 --> 00:31:36.060
it's named FU or uh, you know,

511
00:31:36.280 --> 00:31:40.260
Johnny wanted this and at least gives you that. Here we also echo,

512
00:31:40.560 --> 00:31:44.180
if you notice this doll sign all capital's home because that is another

513
00:31:44.180 --> 00:31:48.540
environment variable that is set by the host, by us, uh, for us that we can use.

514
00:31:49.850 --> 00:31:53.510
And then line 13 is just echoing it out.

515
00:31:53.530 --> 00:31:55.750
But here you notice when we're calling a function,

516
00:31:56.410 --> 00:31:59.790
what's most important here is we're using a dolls sign parenthesis in here to

517
00:31:59.790 --> 00:32:04.070
execute the function because instead of expanding a variable or putting it out,

518
00:32:04.450 --> 00:32:08.150
the parentheses basically execute the function for us and then whatever the

519
00:32:08.150 --> 00:32:08.770
results are,

520
00:32:08.770 --> 00:32:12.190
it puts in place of it and it's the same form the get home directory.

521
00:32:13.760 --> 00:32:17.050
Okay? So functions could take parameters just like commands.

522
00:32:17.590 --> 00:32:22.490
And in here we do a list files function and then we echo

523
00:32:22.680 --> 00:32:26.810
this Dollas sign pound basically gives you the number of arguments that are

524
00:32:26.810 --> 00:32:28.930
passed into this. So if you pass in five arguments,

525
00:32:29.480 --> 00:32:32.170
this line 18 is gonna say there are five arguments.

526
00:32:34.310 --> 00:32:37.290
How do you get an argument in in your function?

527
00:32:37.350 --> 00:32:41.080
You use the dollas sign one if you want the second argument you would do doll

528
00:32:41.080 --> 00:32:44.200
sign two if the third argument dolls sign three, and so on and so on.

529
00:32:45.010 --> 00:32:49.510
And so here we've recreated the LS command, we've basically wrapped it,

530
00:32:49.550 --> 00:32:50.310
I shouldn't say we recreated,

531
00:32:50.310 --> 00:32:55.310
we wrapped the LS command and we're saying go ahead and whatever directory we

532
00:32:55.420 --> 00:32:57.990
pass into list files function, go

533
00:32:59.890 --> 00:33:03.010
ahead and use the Ls and the al flag.

534
00:33:03.740 --> 00:33:06.560
And here on line 23, we're calling the function,

535
00:33:07.540 --> 00:33:10.120
but we're passing this get home function to it.

536
00:33:10.500 --> 00:33:13.640
So we're using a function to pass a,

537
00:33:13.780 --> 00:33:16.680
to give us a parameter to pass to one function.

538
00:33:17.220 --> 00:33:21.840
You could have just as easily made a string here called slash home slash

539
00:33:22.020 --> 00:33:26.160
EC two user. But in this case I wanted to show how you can use them, uh,

540
00:33:26.160 --> 00:33:30.790
both like this. Okay? So let's get outta here and let's run it.

541
00:33:30.790 --> 00:33:33.030
We'll see it in action. So shift colon Q

542
00:33:34.640 --> 00:33:37.930
up arrow control A and let's run it.

543
00:33:38.500 --> 00:33:42.780
So you can see this first line up here is hello

544
00:33:43.500 --> 00:33:47.660
I am ip which is, this is the host name. This is our e C two host name.

545
00:33:48.820 --> 00:33:52.760
And then here you can see I live in the home ec two user home directed and that

546
00:33:52.760 --> 00:33:55.080
is that built in home variable that we're using

547
00:33:57.370 --> 00:34:01.340
here you can see there are one arguments we only passed in the get whatever the

548
00:34:01.360 --> 00:34:04.620
get home function return, which was just one parameter.

549
00:34:05.400 --> 00:34:07.820
And the argument one that we passed in was this.

550
00:34:08.360 --> 00:34:13.180
And then remember we called LS minus al on dollar sign one and then here we go,

551
00:34:13.200 --> 00:34:17.610
we, we got the printout of the C two user home director.

552
00:34:18.780 --> 00:34:23.740
Okay, let me clear the screen. Okay,

553
00:34:24.000 --> 00:34:28.860
so now it is time to put everything we've learned so far about this, you know,

554
00:34:28.860 --> 00:34:32.260
quick crash course and bash in some of these fundamentals and we're gonna make a

555
00:34:32.260 --> 00:34:34.500
simple guessing game. You know the ones that say like,

556
00:34:34.500 --> 00:34:37.540
guess the number one through 10, one through a hundred, whatever.

557
00:34:37.960 --> 00:34:39.660
And then if it you pick a number, it says too high,

558
00:34:39.660 --> 00:34:44.470
too low until you get it and then you get it, you win. Okay,

559
00:34:44.470 --> 00:34:48.630
so I provided the full source code for this uh, game. It's in the section five,

560
00:34:49.210 --> 00:34:51.790
but I thought we'd try to do this one together from scratch.

561
00:34:51.970 --> 00:34:56.790
And so we're gonna open up and then I'm gonna just call it GG for

562
00:34:56.790 --> 00:34:58.420
guessing game. And we're gonna,

563
00:34:58.480 --> 00:35:02.820
I'm gonna do this one not in the clone repository but outside in my home

564
00:35:02.820 --> 00:35:06.900
directory and we'll go ahead and do that. And the first command,

565
00:35:06.900 --> 00:35:09.140
as you guys know, bin Bash.

566
00:35:11.130 --> 00:35:13.630
And so the first thing we're gonna do is we need a number, right?

567
00:35:13.650 --> 00:35:17.630
So we need computer to generate a number for us and

568
00:35:18.630 --> 00:35:20.740
we're gonna do a random number obviously.

569
00:35:21.040 --> 00:35:23.660
And so the double parenthesis,

570
00:35:23.820 --> 00:35:25.860
remember cause we need to do a mathematical operation

571
00:35:31.410 --> 00:35:34.070
and I can't spell random, but we'll try that again so random.

572
00:35:34.650 --> 00:35:36.510
And then we're gonna use this modus command,

573
00:35:36.510 --> 00:35:40.120
which basically means we're gonna divide it by 10 because what we want it to do

574
00:35:40.120 --> 00:35:41.400
is we don't want the number we're gonna do,

575
00:35:41.410 --> 00:35:44.000
we're gonna pick a number one through 10 to guess and we don't want the number

576
00:35:44.020 --> 00:35:48.080
to be ever be larger than 10 and we can't let it be zero or ne, you know,

577
00:35:48.400 --> 00:35:48.980
whatever negative.

578
00:35:48.980 --> 00:35:53.900
So we're gonna say plus one here to make sure that we are

579
00:35:54.040 --> 00:35:57.540
within range and we'll close the double parenthesis.

580
00:35:57.640 --> 00:36:01.660
So random by the way, you can see the dollas sign random because that is a,

581
00:36:02.280 --> 00:36:04.580
I'm sorry, that is a built-in environment variable as well.

582
00:36:04.840 --> 00:36:07.580
And it just gives us a random number. We'll do modules 10,

583
00:36:07.580 --> 00:36:12.300
which means divide it and by 10 should always give us a number between one and

584
00:36:12.300 --> 00:36:13.780
10 with the plus one. All right,

585
00:36:14.700 --> 00:36:17.950
so now we're gonna tell the user,

586
00:36:18.050 --> 00:36:20.670
the game player what we're gonna say and we're gonna say guess the number

587
00:36:20.780 --> 00:36:25.080
between one and 10 guests, a number from one to 10.

588
00:36:27.620 --> 00:36:29.760
And then we're gonna say, guess we're gonna have,

589
00:36:29.760 --> 00:36:31.320
we're gonna keep track of the guests.

590
00:36:32.150 --> 00:36:33.850
And so right now we're just gonna say the guess is zero.

591
00:36:33.850 --> 00:36:35.650
This is what the user will guess in.

592
00:36:35.990 --> 00:36:38.610
Now we're gonna use a wild loop here because we want to,

593
00:36:38.670 --> 00:36:41.250
we want the script to keep asking us over again.

594
00:36:41.250 --> 00:36:44.890
We want it to say too high or too low. And that'll work with the wild loop.

595
00:36:45.000 --> 00:36:47.210
When the condition's met, that means we won the game.

596
00:36:47.980 --> 00:36:50.560
So we'll do while we'll do the brackets cuz we're gonna test,

597
00:36:52.720 --> 00:36:57.030
we'll say while our guests does not equal.

598
00:36:57.130 --> 00:37:00.710
So the dash n e does not equal the number,

599
00:37:01.080 --> 00:37:05.830
which is what the random number is, right? While that

600
00:37:07.340 --> 00:37:08.360
we want to do this,

601
00:37:09.100 --> 00:37:13.960
we wanna read the guess in and remember Reid is gonna read from

602
00:37:14.120 --> 00:37:16.480
whatever the person types in. So they'll be typing in the guess here.

603
00:37:17.020 --> 00:37:19.960
And then here what we want to do is we wanna say if,

604
00:37:23.900 --> 00:37:24.733
if guess

605
00:37:26.420 --> 00:37:29.460
is less than the number that was random,

606
00:37:32.960 --> 00:37:35.630
then we want to echo it's too low,

607
00:37:38.010 --> 00:37:39.360
right? And then fi,

608
00:37:40.020 --> 00:37:44.640
we also wanna say if we'll do another if statement here and we're gonna say,

609
00:37:44.820 --> 00:37:45.920
if guess

610
00:37:49.570 --> 00:37:54.310
is greater than the number that was randomly chosen

611
00:37:54.410 --> 00:37:55.243
for us,

612
00:37:56.360 --> 00:37:59.730
then we're gonna echo it is too high

613
00:38:01.430 --> 00:38:03.180
close, oops, close our if block

614
00:38:04.730 --> 00:38:06.910
and then we're done with our wild loop.

615
00:38:06.970 --> 00:38:11.900
So that means if we have finished this wild loop,

616
00:38:11.900 --> 00:38:15.620
that means that the, the guest number is equal to the number,

617
00:38:15.720 --> 00:38:20.510
the guest is equal to the number, which means that you have won,

618
00:38:20.970 --> 00:38:21.803
you have won the game.

619
00:38:23.760 --> 00:38:27.700
And here we can just do an exit cleanly just for safety.

620
00:38:27.880 --> 00:38:30.500
And then here we're gonna use a new command,

621
00:38:30.750 --> 00:38:32.780
which is gonna be the shift colon X.

622
00:38:33.810 --> 00:38:38.360
And what this will do was write and quit at the same time. Okay?

623
00:38:39.640 --> 00:38:41.780
If I tried to just run this like this,

624
00:38:42.920 --> 00:38:45.460
sh you can see permission denied.

625
00:38:45.520 --> 00:38:50.340
And this is because if we look at GS H, you could see, oops, sorry,

626
00:38:50.960 --> 00:38:55.100
let me do the dash L for the Longview. You could see that it's not executable,

627
00:38:55.290 --> 00:38:57.420
it's read and writeable, but it's not executable.

628
00:38:57.560 --> 00:39:00.620
We don't have an X in our permission.

629
00:39:01.230 --> 00:39:05.720
So one way you could do this is you could do a chima plus x

630
00:39:07.380 --> 00:39:09.440
the file you want to do in this case the GG h.

631
00:39:09.620 --> 00:39:13.740
And then let me clear the screen here and let's see if it works.

632
00:39:13.920 --> 00:39:16.940
So we'll do dot slash gg sh,

633
00:39:16.940 --> 00:39:19.780
which means we want to run it exactly where the file is.

634
00:39:20.320 --> 00:39:23.660
And because it's now executable and we have that shebang on line one,

635
00:39:23.660 --> 00:39:27.170
it should just work. Okay, so it says guess the number from one to 10.

636
00:39:27.670 --> 00:39:31.210
I'm gonna say five. So the number's too high. Say three,

637
00:39:31.990 --> 00:39:36.010
two A was two, we won the game. Okay?

638
00:39:36.150 --> 00:39:38.210
Now I know that's more of a contrived example,

639
00:39:38.350 --> 00:39:42.010
but you put all the paradigms together, you put a wild loop, variables,

640
00:39:42.010 --> 00:39:45.370
mathematical expressions, exits, echoes.

641
00:39:46.040 --> 00:39:49.170
That should be a good foundation to get you going riding.

642
00:39:49.230 --> 00:39:51.170
And obviously the rest of this videos going forward,

643
00:39:51.170 --> 00:39:53.690
you're gonna do a little bit more of the administrative type of task.

644
00:39:53.750 --> 00:39:57.450
Now that you have a bash fundamental, okay, let's go back and do a quick recap.

645
00:39:59.360 --> 00:40:01.850
This was a long video, um, lot to cover.

646
00:40:01.910 --> 00:40:05.650
So appreciate you sticking with it and going through all the examples with me.

647
00:40:06.110 --> 00:40:08.170
Uh, I promise you it'll pay off in the long run.

648
00:40:08.630 --> 00:40:12.250
So we learned about the Bash or Born Again Shell.

649
00:40:12.950 --> 00:40:17.130
You also were introduced to concepts like arrays if and switch statements,

650
00:40:17.770 --> 00:40:19.590
functions, variables. Finally,

651
00:40:19.650 --> 00:40:23.030
you created a guessing game script using all the Bash fundamentals you learned.

652
00:40:24.470 --> 00:40:25.303
In the next video,

653
00:40:25.310 --> 00:40:29.130
you create backups with a bash script and use Kron to automate it. See you soon.
