1
00:00:00,090 --> 00:00:01,110
In this lesson,

2
00:00:01,110 --> 00:00:02,760
we're going to discuss the concepts

3
00:00:02,760 --> 00:00:05,880
that lay the groundwork for writing your own scripts.

4
00:00:05,880 --> 00:00:07,860
And we're going to do that by looking at scripting

5
00:00:07,860 --> 00:00:09,780
and programming fundamentals.

6
00:00:09,780 --> 00:00:11,760
Now, before we start to create your own scripts

7
00:00:11,760 --> 00:00:12,750
with bash though,

8
00:00:12,750 --> 00:00:13,980
you need to start getting familiar

9
00:00:13,980 --> 00:00:15,600
with some of the basic concepts

10
00:00:15,600 --> 00:00:16,860
that are shared by many scripting

11
00:00:16,860 --> 00:00:19,020
and programming languages used today.

12
00:00:19,020 --> 00:00:20,910
Bash is not only the default shell

13
00:00:20,910 --> 00:00:22,440
of the Linux operating system,

14
00:00:22,440 --> 00:00:24,870
but it's also a powerful scripting language.

15
00:00:24,870 --> 00:00:26,700
Now bash scripting is a skill

16
00:00:26,700 --> 00:00:29,430
that any system administrator really needs to learn

17
00:00:29,430 --> 00:00:31,080
because it's something that's going to help you

18
00:00:31,080 --> 00:00:33,090
leverage your time and really automate

19
00:00:33,090 --> 00:00:35,520
a lot of the things you do on a daily basis.

20
00:00:35,520 --> 00:00:36,810
By using bash scripting,

21
00:00:36,810 --> 00:00:39,030
you can leverage Linux as existing tools

22
00:00:39,030 --> 00:00:41,550
to get things done much quicker.

23
00:00:41,550 --> 00:00:45,360
Essentially, any program tool, utility or system function

24
00:00:45,360 --> 00:00:47,040
that you can call it the command line,

25
00:00:47,040 --> 00:00:50,010
you can also invoke from within a bash script.

26
00:00:50,010 --> 00:00:51,690
Similarly, bash scripts

27
00:00:51,690 --> 00:00:53,820
also support modern programming elements

28
00:00:53,820 --> 00:00:55,680
like loops and conditional statements

29
00:00:55,680 --> 00:00:58,920
to enhance the logic of the task that are being automated.

30
00:00:58,920 --> 00:01:01,320
Just as the commands inside the command line interface

31
00:01:01,320 --> 00:01:02,760
have a certain syntax,

32
00:01:02,760 --> 00:01:05,760
your scripting languages also have specific syntaxes

33
00:01:05,760 --> 00:01:07,200
that have to be used.

34
00:01:07,200 --> 00:01:08,700
When I talk about syntax,

35
00:01:08,700 --> 00:01:10,230
I'm simply referring to the rules

36
00:01:10,230 --> 00:01:12,630
that define how you're going to write your code.

37
00:01:12,630 --> 00:01:15,750
Each scripting and programming language has its own syntax,

38
00:01:15,750 --> 00:01:18,000
but many share a few commonalities.

39
00:01:18,000 --> 00:01:20,220
Because the Bash is really the default

40
00:01:20,220 --> 00:01:22,110
inside the Linux operating system,

41
00:01:22,110 --> 00:01:24,720
you're going to see that the syntax of the bash script

42
00:01:24,720 --> 00:01:26,550
is very similar to what you learned

43
00:01:26,550 --> 00:01:28,530
when you inputted things line by line

44
00:01:28,530 --> 00:01:30,630
within the command line environment.

45
00:01:30,630 --> 00:01:34,260
Now your bash script may contain shell specific instructions

46
00:01:34,260 --> 00:01:36,810
that are not compatible with other Linux shells though.

47
00:01:36,810 --> 00:01:38,190
Remember, in Linux,

48
00:01:38,190 --> 00:01:40,860
you can choose what shell environment you want to use.

49
00:01:40,860 --> 00:01:42,600
We're using bash in this course,

50
00:01:42,600 --> 00:01:44,340
but you could really use anything you want

51
00:01:44,340 --> 00:01:47,460
such as the C shell, Z shell or others.

52
00:01:47,460 --> 00:01:48,690
To specify that your script

53
00:01:48,690 --> 00:01:50,430
is being written for the bash shell,

54
00:01:50,430 --> 00:01:55,430
you need to add the line #bang/bin/bash

55
00:01:55,620 --> 00:01:57,450
at the beginning of each script.

56
00:01:57,450 --> 00:01:59,010
Anytime you hear the word bang,

57
00:01:59,010 --> 00:02:01,230
that means the exclamation mark.

58
00:02:01,230 --> 00:02:03,450
Now this line will instruct the operating system

59
00:02:03,450 --> 00:02:05,280
to use the bash shell interpreter

60
00:02:05,280 --> 00:02:06,870
when they execute this script

61
00:02:06,870 --> 00:02:08,729
on an incompatible Linux shell,

62
00:02:08,729 --> 00:02:12,540
and that way your script can still operate on that system.

63
00:02:12,540 --> 00:02:14,550
Now, another thing we're going to do inside of our scripts

64
00:02:14,550 --> 00:02:15,810
is assign variables.

65
00:02:15,810 --> 00:02:18,570
And we assign variables to symbolically associate

66
00:02:18,570 --> 00:02:21,360
a specific piece of information with a given name,

67
00:02:21,360 --> 00:02:22,710
and then we can refer to that name

68
00:02:22,710 --> 00:02:25,140
when we want to get that piece of information.

69
00:02:25,140 --> 00:02:27,690
Lots of different programming languages like C

70
00:02:27,690 --> 00:02:29,880
are going to require you to define the type of variable

71
00:02:29,880 --> 00:02:31,920
before you assign a value to it.

72
00:02:31,920 --> 00:02:34,170
This includes things like integers, floats,

73
00:02:34,170 --> 00:02:36,300
strings and other characteristics.

74
00:02:36,300 --> 00:02:38,370
Essentially, these types define exactly

75
00:02:38,370 --> 00:02:40,784
what kind of information that variable can hold.

76
00:02:40,784 --> 00:02:44,880
However, in bash, you don't have to declare variable types.

77
00:02:44,880 --> 00:02:48,060
All bash variables are treated as strings by default,

78
00:02:48,060 --> 00:02:50,340
and therefore you can put any kind of data you want

79
00:02:50,340 --> 00:02:52,440
inside of that variable.

80
00:02:52,440 --> 00:02:54,660
Now, one unique thing about assigning values

81
00:02:54,660 --> 00:02:56,160
in bash to a variable

82
00:02:56,160 --> 00:02:58,740
is that you're going to make sure there are no spaces

83
00:02:58,740 --> 00:03:01,110
before or after the equal sign.

84
00:03:01,110 --> 00:03:03,630
For example, if I was assigning Jason Dion

85
00:03:03,630 --> 00:03:05,370
to the variable myname,

86
00:03:05,370 --> 00:03:09,570
I would type in myname equals 'Jason Dion'

87
00:03:09,570 --> 00:03:13,470
and that's it, no spaces except between Jason and Dion.

88
00:03:13,470 --> 00:03:14,880
And it's okay to have the space there

89
00:03:14,880 --> 00:03:17,130
because I have the quotes around that string.

90
00:03:17,970 --> 00:03:20,190
Now, after you assign a value to a variable,

91
00:03:20,190 --> 00:03:22,800
you can reference that variable later on in your code,

92
00:03:22,800 --> 00:03:24,480
so you don't have to hard code these values

93
00:03:24,480 --> 00:03:26,220
into your script's logic.

94
00:03:26,220 --> 00:03:29,070
The act of referencing or retrieving the valuable variable

95
00:03:29,070 --> 00:03:32,430
is known as substitution or parameter expansion.

96
00:03:32,430 --> 00:03:34,590
Now, when you're referencing variables in bash,

97
00:03:34,590 --> 00:03:36,510
you're going to do this by adding a dollar sign

98
00:03:36,510 --> 00:03:38,460
to the beginning of the variable name.

99
00:03:38,460 --> 00:03:42,030
For example, if I wanted to display myname to the screen,

100
00:03:42,030 --> 00:03:44,613
I would do that by typing echo $myname,

101
00:03:45,540 --> 00:03:47,370
and this will print out Jason Dion

102
00:03:47,370 --> 00:03:50,400
into your console or command line environment.

103
00:03:50,400 --> 00:03:53,010
So note that when you're assigning the variable,

104
00:03:53,010 --> 00:03:54,330
you don't use the dollar sign,

105
00:03:54,330 --> 00:03:56,640
but when you're pulling information from that variable,

106
00:03:56,640 --> 00:03:58,530
you use the dollar sign.

107
00:03:58,530 --> 00:04:00,180
Now, in addition to storing data

108
00:04:00,180 --> 00:04:02,010
and retrieving data from a variable,

109
00:04:02,010 --> 00:04:04,830
there's other operations you can do with these variables

110
00:04:04,830 --> 00:04:07,860
to create tasks and outcomes that you're looking for.

111
00:04:07,860 --> 00:04:09,540
For example, you might need

112
00:04:09,540 --> 00:04:12,300
to be able to evaluate the condition of a variable

113
00:04:12,300 --> 00:04:14,220
and what's stored inside of it.

114
00:04:14,220 --> 00:04:16,079
This is known as a comparison,

115
00:04:16,079 --> 00:04:17,940
and we do this using operators.

116
00:04:17,940 --> 00:04:20,670
Now operators are the object that can evaluate expressions

117
00:04:20,670 --> 00:04:22,650
in a variety of different ways.

118
00:04:22,650 --> 00:04:23,970
When we look at operands,

119
00:04:23,970 --> 00:04:26,910
these are the values that are being operated on.

120
00:04:26,910 --> 00:04:29,160
So there's lots of different kinds of operators

121
00:04:29,160 --> 00:04:31,380
that we can apply inside of bash.

122
00:04:31,380 --> 00:04:34,380
We have arithmetic operators, comparison operators,

123
00:04:34,380 --> 00:04:37,140
logical operators, and string operators.

124
00:04:37,140 --> 00:04:39,060
Let's take a quick look at each of these.

125
00:04:39,060 --> 00:04:41,250
First, we have arithmetic operators.

126
00:04:41,250 --> 00:04:44,610
These include things like adding, subtracting, multiplying,

127
00:04:44,610 --> 00:04:47,970
and dividing and other more advanced mathematical functions.

128
00:04:47,970 --> 00:04:50,610
For example, if I had variable one and variable two,

129
00:04:50,610 --> 00:04:52,320
and I wanted to add them together,

130
00:04:52,320 --> 00:04:55,110
I can do that by using a double parenthesis around them.

131
00:04:55,110 --> 00:04:57,750
And so I would say dollar sign, parenthesis, parenthesis

132
00:04:57,750 --> 00:05:00,870
var one plus var two, parenthesis parenthesis.

133
00:05:00,870 --> 00:05:02,970
And this will take the values of variable one

134
00:05:02,970 --> 00:05:05,040
and variable two and add them together

135
00:05:05,040 --> 00:05:06,300
and display that to the screen

136
00:05:06,300 --> 00:05:08,670
if it was inside of an echo command.

137
00:05:08,670 --> 00:05:10,860
Now comparison operators are going to be used

138
00:05:10,860 --> 00:05:12,420
to check if things are equal,

139
00:05:12,420 --> 00:05:14,940
if something is less than or greater than the other,

140
00:05:14,940 --> 00:05:16,560
or other things like that.

141
00:05:16,560 --> 00:05:18,960
For example, if I'm going to do a comparison

142
00:05:18,960 --> 00:05:21,270
to see if variable one is greater than

143
00:05:21,270 --> 00:05:22,950
or equal to variable two,

144
00:05:22,950 --> 00:05:25,860
I would do that by typing bracket, dollar sign,

145
00:05:25,860 --> 00:05:30,860
var one -ge dollar sign var two, end bracket.

146
00:05:31,020 --> 00:05:34,410
Now notice here I'm using brackets instead of parentheses,

147
00:05:34,410 --> 00:05:36,990
that's because I'm using a comparison operator.

148
00:05:36,990 --> 00:05:38,850
And the return value from this comparison

149
00:05:38,850 --> 00:05:42,750
is either a one if it's true or a zero, if it's false.

150
00:05:42,750 --> 00:05:45,060
The next one we have is a logical operator.

151
00:05:45,060 --> 00:05:46,500
And logical operators are used

152
00:05:46,500 --> 00:05:48,270
to connect multiple values together

153
00:05:48,270 --> 00:05:49,860
so they can be evaluated.

154
00:05:49,860 --> 00:05:53,490
Normally these are things like, and, or, and not.

155
00:05:53,490 --> 00:05:56,490
For example, if I wanted to test two different conditions

156
00:05:56,490 --> 00:05:58,350
and then see if they are both true,

157
00:05:58,350 --> 00:06:00,060
I would use an and operator,

158
00:06:00,060 --> 00:06:03,300
which is symbolized by the ampersand ampersand sign.

159
00:06:03,300 --> 00:06:07,093
So I might do something like bracket dollar sign var one

160
00:06:07,093 --> 00:06:10,410
ge dollar sign var two, bracket,

161
00:06:10,410 --> 00:06:14,940
ampersand, ampersand bracket, dollar sign var three, -le

162
00:06:14,940 --> 00:06:17,460
dollar sign var four, bracket.

163
00:06:17,460 --> 00:06:20,520
This says, tell me is variable one

164
00:06:20,520 --> 00:06:22,440
greater than or equal two variable two

165
00:06:22,440 --> 00:06:26,325
and is variable three less than or equal two variable four.

166
00:06:26,325 --> 00:06:28,290
This is how you connect things together

167
00:06:28,290 --> 00:06:30,553
by using ands, ors and nots.

168
00:06:30,553 --> 00:06:32,280
When you want to use a not,

169
00:06:32,280 --> 00:06:34,920
it's going to be a bang equal sign.

170
00:06:34,920 --> 00:06:37,980
If you want to use an or, it's going to be two pipes,

171
00:06:37,980 --> 00:06:40,470
which is the up and down straight line characters,

172
00:06:40,470 --> 00:06:42,510
right above your enter key.

173
00:06:42,510 --> 00:06:44,640
Next, we have string operators.

174
00:06:44,640 --> 00:06:47,070
Now string operators are going to be used in operations

175
00:06:47,070 --> 00:06:49,440
that manipulate strings in various ways,

176
00:06:49,440 --> 00:06:51,120
including concatenating strings,

177
00:06:51,120 --> 00:06:53,190
returning a specific character in a string,

178
00:06:53,190 --> 00:06:55,410
which is known as slicing or verifying

179
00:06:55,410 --> 00:06:58,320
if special characters exist inside of a string.

180
00:06:58,320 --> 00:07:01,380
For example, if I wanted to concatenate two variables

181
00:07:01,380 --> 00:07:04,890
and put them together, I can take dollar sign var one,

182
00:07:04,890 --> 00:07:07,770
dollar sign var two, with no spaces in between them,

183
00:07:07,770 --> 00:07:10,200
and this will put them both together as a single word

184
00:07:10,200 --> 00:07:12,090
or single string when being displayed

185
00:07:12,090 --> 00:07:13,980
to the command line environment.

186
00:07:13,980 --> 00:07:16,980
Now, the next thing we want to talk about is a string literal.

187
00:07:16,980 --> 00:07:19,230
A string literal is any fixed value

188
00:07:19,230 --> 00:07:22,200
that represents a string of text within your source code.

189
00:07:22,200 --> 00:07:24,420
String literals are enclosed either single

190
00:07:24,420 --> 00:07:25,797
or double quotation marks,

191
00:07:25,797 --> 00:07:28,050
and as long as you're using them consistently,

192
00:07:28,050 --> 00:07:30,690
you can use either single or double quotation marks.

193
00:07:30,690 --> 00:07:33,210
It really doesn't matter. It's up to you.

194
00:07:33,210 --> 00:07:35,790
However, there are some cases where double quotes

195
00:07:35,790 --> 00:07:37,350
won't preserve the literal value

196
00:07:37,350 --> 00:07:39,750
of all the characters within those quotes.

197
00:07:39,750 --> 00:07:42,000
For example, let's go back to the example

198
00:07:42,000 --> 00:07:43,710
of having myname as a variable

199
00:07:43,710 --> 00:07:46,140
that contains Jason, space, Dion.

200
00:07:46,140 --> 00:07:48,240
Now let's say I wanted to substitute this variable

201
00:07:48,240 --> 00:07:50,550
into a larger literal strength.

202
00:07:50,550 --> 00:07:51,720
I can do that by typing

203
00:07:51,720 --> 00:07:55,293
echo " my name is dollar sign, myname"

204
00:07:57,180 --> 00:07:58,200
and this is going to print

205
00:07:58,200 --> 00:08:01,320
My name is Jason Dion over to your screen.

206
00:08:01,320 --> 00:08:03,750
Now, on the other hand, if I use single quotes

207
00:08:03,750 --> 00:08:08,223
and typed echo 'My name is dollar sign, myname'

208
00:08:09,180 --> 00:08:10,650
this is going to print out to the screen,

209
00:08:10,650 --> 00:08:13,260
My name is dollar sign myname

210
00:08:13,260 --> 00:08:15,630
because it's not actually parsing that variable

211
00:08:15,630 --> 00:08:18,060
because I use single quotes instead of double quotes

212
00:08:18,060 --> 00:08:19,994
in this particular example.

213
00:08:19,994 --> 00:08:21,780
In general, it's a great idea

214
00:08:21,780 --> 00:08:24,000
to always put quotes around your strings

215
00:08:24,000 --> 00:08:25,800
when you're assigning them to a variable.

216
00:08:25,800 --> 00:08:27,150
This is especially important

217
00:08:27,150 --> 00:08:29,160
if you have spaces in the variable,

218
00:08:29,160 --> 00:08:32,220
such as Jason space Dion, that I was assigning

219
00:08:32,220 --> 00:08:34,200
into the myname variable.

220
00:08:34,200 --> 00:08:35,280
If you don't do that,

221
00:08:35,280 --> 00:08:37,320
it's only going to sign the first word

222
00:08:37,320 --> 00:08:38,850
instead of the entire phrase.

223
00:08:38,850 --> 00:08:40,860
So in that case, I'd only have Jason stored

224
00:08:40,860 --> 00:08:43,110
instead of Jason space Dion.

225
00:08:43,110 --> 00:08:45,690
Now, if it's a single word, you don't need quotes.

226
00:08:45,690 --> 00:08:48,750
But again, it's just a good practice to always use quotes

227
00:08:48,750 --> 00:08:51,450
around what you're setting your variable names to.

228
00:08:51,450 --> 00:08:53,670
Now in any language, including bash,

229
00:08:53,670 --> 00:08:56,100
there are certain characters that have special meanings.

230
00:08:56,100 --> 00:08:57,990
And these characters are basically what we call

231
00:08:57,990 --> 00:08:59,460
reserved characters.

232
00:08:59,460 --> 00:09:01,320
Let's talk about a few of them here.

233
00:09:01,320 --> 00:09:03,270
First, we have an escape character.

234
00:09:03,270 --> 00:09:04,680
Now an escape character is used

235
00:09:04,680 --> 00:09:06,330
to remove that special meaning

236
00:09:06,330 --> 00:09:08,137
so that any character can be used literally

237
00:09:08,137 --> 00:09:11,580
rather than as an interpreted thing by the system.

238
00:09:11,580 --> 00:09:14,670
For example, you saw the fact that we use hashtags

239
00:09:14,670 --> 00:09:16,500
as a way to start a comment on a line

240
00:09:16,500 --> 00:09:18,600
or to describe what this script is

241
00:09:18,600 --> 00:09:21,870
by doing the hashtag bang at the beginning of the script.

242
00:09:21,870 --> 00:09:23,760
Well, what if I wanted to tell you

243
00:09:23,760 --> 00:09:25,320
that I wanted to be number one

244
00:09:25,320 --> 00:09:27,510
and put that hashtag in front of the one

245
00:09:27,510 --> 00:09:29,160
when I display it to the screen.

246
00:09:29,160 --> 00:09:31,380
If I did that normally, it's just going to go ahead

247
00:09:31,380 --> 00:09:34,770
and process everything at the end of that line as a comment.

248
00:09:34,770 --> 00:09:37,530
So I need to first escape this special meaning

249
00:09:37,530 --> 00:09:39,510
from that hashtag character.

250
00:09:39,510 --> 00:09:42,060
To do this, I can use an escape character

251
00:09:42,060 --> 00:09:44,160
such as a single back slash.

252
00:09:44,160 --> 00:09:45,900
This single back slash will be put

253
00:09:45,900 --> 00:09:47,820
right in front of that character.

254
00:09:47,820 --> 00:09:50,580
And so if I said, I am number one,

255
00:09:50,580 --> 00:09:53,700
I'm going to write that as I am single back slash,

256
00:09:53,700 --> 00:09:57,210
pound sign, one, and that way it'll process

257
00:09:57,210 --> 00:10:01,110
as the pound sign and not as a comment character.

258
00:10:01,110 --> 00:10:02,580
This is the way you can escape something

259
00:10:02,580 --> 00:10:04,740
and take away that special meaning.

260
00:10:04,740 --> 00:10:08,010
Another way to do this is by using single quotation marks.

261
00:10:08,010 --> 00:10:12,240
So if I put single quotation marks around that string of

262
00:10:12,240 --> 00:10:14,730
I am number one, it's still going to process

263
00:10:14,730 --> 00:10:16,680
as I am number one.

264
00:10:16,680 --> 00:10:18,780
Alternatively, if you wanted to use double quotes

265
00:10:18,780 --> 00:10:19,920
or no quotes,

266
00:10:19,920 --> 00:10:22,320
then you're going to have to use that escape character

267
00:10:22,320 --> 00:10:24,660
of using that single back slash.

268
00:10:24,660 --> 00:10:26,970
The next thing we're going to talk about is an array.

269
00:10:26,970 --> 00:10:29,190
Now, an array enables you to store multiple values

270
00:10:29,190 --> 00:10:30,720
in a single variable.

271
00:10:30,720 --> 00:10:33,690
This can make your code much easier to read and maintain

272
00:10:33,690 --> 00:10:34,523
because you don't have

273
00:10:34,523 --> 00:10:35,970
to have a bunch of different variables.

274
00:10:35,970 --> 00:10:38,700
You can have one that contains everything you need.

275
00:10:38,700 --> 00:10:40,560
For example, you might want to perform

276
00:10:40,560 --> 00:10:42,120
a single mathematical operation

277
00:10:42,120 --> 00:10:44,040
on dozens of different values.

278
00:10:44,040 --> 00:10:46,350
And instead of creating a variable for each value,

279
00:10:46,350 --> 00:10:48,480
you can create an array to simplify your code

280
00:10:48,480 --> 00:10:51,720
and store all those values in different parts of the array.

281
00:10:51,720 --> 00:10:53,100
Another benefit of arrays

282
00:10:53,100 --> 00:10:54,840
is that you can easily update their values

283
00:10:54,840 --> 00:10:57,060
throughout the code because it's a single thing

284
00:10:57,060 --> 00:10:59,286
instead of having multiple different variables.

285
00:10:59,286 --> 00:11:03,060
Now, when you do compound assignments inside bash arrays,

286
00:11:03,060 --> 00:11:04,260
you're going to use parentheses

287
00:11:04,260 --> 00:11:06,805
with each value being separated by a space.

288
00:11:06,805 --> 00:11:10,710
For example, let's say I created an array called my array

289
00:11:10,710 --> 00:11:12,990
and I want to store three words inside of it.

290
00:11:12,990 --> 00:11:15,330
Jason, Dion, and training.

291
00:11:15,330 --> 00:11:19,380
To do this, I'm going to type in my array equals parenthesis,

292
00:11:19,380 --> 00:11:21,570
double quote, Jason, double quote,

293
00:11:21,570 --> 00:11:23,550
double quote, Dion, double quote,

294
00:11:23,550 --> 00:11:26,940
double quote, training, double quote, and parentheses.

295
00:11:26,940 --> 00:11:29,010
Now, in addition to assigning the array all at once

296
00:11:29,010 --> 00:11:30,150
like I did here,

297
00:11:30,150 --> 00:11:32,130
I can also individually assign things

298
00:11:32,130 --> 00:11:35,010
into the specific values based on their indexes

299
00:11:35,010 --> 00:11:36,630
and I place that in a bracket.

300
00:11:36,630 --> 00:11:39,420
So for example, if I wanted to get the word, Jason,

301
00:11:39,420 --> 00:11:43,020
I would do my array bracket zero bracket equals,

302
00:11:43,020 --> 00:11:45,030
double quote, Jason double quote,

303
00:11:45,030 --> 00:11:47,730
and that assigns the word Jason into space zero,

304
00:11:47,730 --> 00:11:50,790
which is the first space in this particular array.

305
00:11:50,790 --> 00:11:52,890
I can do the same thing with Dion and training.

306
00:11:52,890 --> 00:11:55,620
by using my array one and my array two,

307
00:11:55,620 --> 00:11:57,630
and putting those values into there.

308
00:11:57,630 --> 00:11:59,160
You can always reference an array

309
00:11:59,160 --> 00:12:00,690
by wrapping it in curly braces

310
00:12:00,690 --> 00:12:02,760
when you want to get information back out of it.

311
00:12:02,760 --> 00:12:05,070
So if I wanted to pull out the name, Jason,

312
00:12:05,070 --> 00:12:08,730
I can do echo dollar sign, curly brace, my array,

313
00:12:08,730 --> 00:12:11,100
bracket, zero bracket, curly brace,

314
00:12:11,100 --> 00:12:14,220
and this would return the value of Jason from position zero,

315
00:12:14,220 --> 00:12:17,180
which is the first position inside of that array.

316
00:12:17,180 --> 00:12:19,350
Now, another thing we need to talk about quickly

317
00:12:19,350 --> 00:12:20,460
is functions.

318
00:12:20,460 --> 00:12:22,770
Now functions are a block of code that you can reuse

319
00:12:22,770 --> 00:12:24,660
to perform a specific task.

320
00:12:24,660 --> 00:12:25,500
This is really useful

321
00:12:25,500 --> 00:12:27,120
when you're trying to write efficient code.

322
00:12:27,120 --> 00:12:28,920
So you don't have to copy and paste things over

323
00:12:28,920 --> 00:12:29,910
and over again.

324
00:12:29,910 --> 00:12:31,380
And so we'll call the function

325
00:12:31,380 --> 00:12:34,260
and somehow to write out the same thing over and over again.

326
00:12:34,260 --> 00:12:36,300
Just like a variable, you can define a function

327
00:12:36,300 --> 00:12:38,640
using a unique name or identifier,

328
00:12:38,640 --> 00:12:40,890
and then you can reference that identifier

329
00:12:40,890 --> 00:12:43,650
to be able to do all the things inside that function

330
00:12:43,650 --> 00:12:45,090
based on its code.

331
00:12:45,090 --> 00:12:48,000
So in bash, there's really two ways to write functions

332
00:12:48,000 --> 00:12:50,010
and both involve placing the desired code

333
00:12:50,010 --> 00:12:52,020
between a set of curly braces.

334
00:12:52,020 --> 00:12:54,990
Now, the first method is going to be something like this.

335
00:12:54,990 --> 00:12:58,890
Function, my function name, curly brace code, curly brace,

336
00:12:58,890 --> 00:13:01,470
and all your code goes inside those curly braces.

337
00:13:01,470 --> 00:13:02,303
Now, if you're familiar

338
00:13:02,303 --> 00:13:04,050
with using object oriented programming

339
00:13:04,050 --> 00:13:06,870
and you come from a background like C or Java,

340
00:13:06,870 --> 00:13:09,570
you might be more comfortable using the second method.

341
00:13:09,570 --> 00:13:12,210
And the second method is my function name,

342
00:13:12,210 --> 00:13:15,240
parenthesis parenthesis, and then curly bracket,

343
00:13:15,240 --> 00:13:17,790
all the code you want, end curly bracket.

344
00:13:17,790 --> 00:13:19,620
Notice both will do the same thing,

345
00:13:19,620 --> 00:13:21,330
it's just a different way of writing it,

346
00:13:21,330 --> 00:13:23,970
depending on which method you prefer.

347
00:13:23,970 --> 00:13:24,840
Now in bash,

348
00:13:24,840 --> 00:13:27,420
when you want to pass arguments into your function,

349
00:13:27,420 --> 00:13:28,770
you're going to do it the same way you would

350
00:13:28,770 --> 00:13:30,665
with a script at the command line.

351
00:13:30,665 --> 00:13:33,690
So if I want to send an argument in such as Jason

352
00:13:33,690 --> 00:13:35,430
into the my function name,

353
00:13:35,430 --> 00:13:39,210
I would simply enter my function name, space, Jason.

354
00:13:39,210 --> 00:13:41,220
And that means Jason is the first argument

355
00:13:41,220 --> 00:13:43,560
that's being passed into my function name.

356
00:13:43,560 --> 00:13:45,780
And then the code will execute upon it.

357
00:13:45,780 --> 00:13:47,010
Now in the world of programming,

358
00:13:47,010 --> 00:13:48,570
we often want to comment our code

359
00:13:48,570 --> 00:13:50,580
so other people can know what we're doing.

360
00:13:50,580 --> 00:13:52,830
And as I said before, the hashtag symbol

361
00:13:52,830 --> 00:13:55,290
is going to indicate that everything after that,

362
00:13:55,290 --> 00:13:57,720
on that particular line is part of the comment

363
00:13:57,720 --> 00:14:00,270
and will not be executed by the system.

364
00:14:00,270 --> 00:14:02,370
You should refrain from commenting on a line of code

365
00:14:02,370 --> 00:14:03,630
whose purpose is obvious,

366
00:14:03,630 --> 00:14:06,450
because too many comments can corrupt your source code.

367
00:14:06,450 --> 00:14:08,130
But in general, it is a good idea

368
00:14:08,130 --> 00:14:10,770
to at least comment what the function is that you're doing

369
00:14:10,770 --> 00:14:13,080
or what the logic of the next several lines is,

370
00:14:13,080 --> 00:14:14,340
so somebody else coming behind you

371
00:14:14,340 --> 00:14:16,350
can understand what you're doing.

372
00:14:16,350 --> 00:14:18,870
Another thing we need to talk about is metacharacters.

373
00:14:18,870 --> 00:14:20,880
Now metacharacters are special characters

374
00:14:20,880 --> 00:14:24,510
that the shell will by default interpret in a certain way.

375
00:14:24,510 --> 00:14:27,330
These characters have to be escaped or enclosed in quotes

376
00:14:27,330 --> 00:14:29,340
if you want them to be interpreted literally.

377
00:14:29,340 --> 00:14:31,470
Similar to the hashtag I just mentioned.

378
00:14:31,470 --> 00:14:33,990
Now let's talk about these different metacharacters,

379
00:14:33,990 --> 00:14:36,120
because it's important to know what characters

380
00:14:36,120 --> 00:14:37,830
are reserved in Linux.

381
00:14:37,830 --> 00:14:39,840
The first one is the greater than symbol,

382
00:14:39,840 --> 00:14:41,910
which is used in output redirection.

383
00:14:41,910 --> 00:14:44,160
We also have the greater than greater than symbol,

384
00:14:44,160 --> 00:14:46,080
which is used in output redirection too,

385
00:14:46,080 --> 00:14:47,340
but in a different manner.

386
00:14:47,340 --> 00:14:48,600
We have the less than symbol,

387
00:14:48,600 --> 00:14:50,490
which is used in input redirection.

388
00:14:50,490 --> 00:14:52,140
We have the less than less than symbol,

389
00:14:52,140 --> 00:14:54,270
which is used for input redirection as well,

390
00:14:54,270 --> 00:14:55,830
but a different context.

391
00:14:55,830 --> 00:14:57,090
We have the up and down bar,

392
00:14:57,090 --> 00:15:00,420
which is used to pipe content from one command into another.

393
00:15:00,420 --> 00:15:01,650
We also have the double quote,

394
00:15:01,650 --> 00:15:04,350
which is used for defining weak literal strings.

395
00:15:04,350 --> 00:15:05,310
We have the single quote,

396
00:15:05,310 --> 00:15:08,160
which is used for defining strong literal strings.

397
00:15:08,160 --> 00:15:09,510
We have the backwards tick mark,

398
00:15:09,510 --> 00:15:11,610
which is used for breaking out of a literal string

399
00:15:11,610 --> 00:15:13,830
to run the command that's inside of it.

400
00:15:13,830 --> 00:15:15,270
We have a single backwards slash

401
00:15:15,270 --> 00:15:17,760
which is used to escape all these other characters.

402
00:15:17,760 --> 00:15:18,990
We have the equal sign,

403
00:15:18,990 --> 00:15:20,610
which is used for variable assignment.

404
00:15:20,610 --> 00:15:21,720
We have the dollar sign,

405
00:15:21,720 --> 00:15:23,700
which is used for variable substitution

406
00:15:23,700 --> 00:15:26,190
and pulling information back out of a variable.

407
00:15:26,190 --> 00:15:29,040
We also have the hashtag, which is used for commenting.

408
00:15:29,040 --> 00:15:30,270
We have the double pipe,

409
00:15:30,270 --> 00:15:32,580
which is used for logical or operations.

410
00:15:32,580 --> 00:15:33,690
The double ampersand,

411
00:15:33,690 --> 00:15:35,940
which is used for logical and operations.

412
00:15:35,940 --> 00:15:38,490
The star, which is used for wildcard matching.

413
00:15:38,490 --> 00:15:39,360
The question mark,

414
00:15:39,360 --> 00:15:40,920
which is used for wildcard matching

415
00:15:40,920 --> 00:15:42,690
being applied on a single character

416
00:15:42,690 --> 00:15:44,520
instead of multiple characters.

417
00:15:44,520 --> 00:15:47,160
We have the brackets which are used for wildcard matching

418
00:15:47,160 --> 00:15:49,860
any applied character between those brackets.

419
00:15:49,860 --> 00:15:51,030
We have the curly braces,

420
00:15:51,030 --> 00:15:53,760
which is used in perimeter substitution and arrays.

421
00:15:53,760 --> 00:15:56,340
We have parentheses, which is used for grouping commands.

422
00:15:56,340 --> 00:15:58,320
We have a single ampersand by itself,

423
00:15:58,320 --> 00:16:01,020
which is used for running a process in the background.

424
00:16:01,020 --> 00:16:02,070
We have the semicolon,

425
00:16:02,070 --> 00:16:03,960
which is used for separating multiple commands

426
00:16:03,960 --> 00:16:05,250
on the same line.

427
00:16:05,250 --> 00:16:07,590
And we have the bang or exclamation character,

428
00:16:07,590 --> 00:16:10,140
which is used in referencing command history.

429
00:16:10,140 --> 00:16:12,180
All of these are considered reserve characters

430
00:16:12,180 --> 00:16:13,230
and metacharacters,

431
00:16:13,230 --> 00:16:14,730
and you should be aware of what they are

432
00:16:14,730 --> 00:16:16,230
and their functions.

433
00:16:16,230 --> 00:16:17,940
Now on Unix and Linux systems,

434
00:16:17,940 --> 00:16:20,520
programs can also pass the value to their parent process

435
00:16:20,520 --> 00:16:21,720
when they terminate.

436
00:16:21,720 --> 00:16:24,990
This is known as an exit code or exit status.

437
00:16:24,990 --> 00:16:27,300
In the Linux world, a status code of zero

438
00:16:27,300 --> 00:16:30,210
indicates that the process executed successfully,

439
00:16:30,210 --> 00:16:33,270
whereas an exit code of one or any number higher than that

440
00:16:33,270 --> 00:16:35,670
means that the process encounters some kind of an error

441
00:16:35,670 --> 00:16:37,140
when it executed.

442
00:16:37,140 --> 00:16:38,970
Now, when you're creating your own scripts,

443
00:16:38,970 --> 00:16:41,490
you want to make sure you take into account exit codes.

444
00:16:41,490 --> 00:16:42,780
Because when you write a script,

445
00:16:42,780 --> 00:16:44,670
it's often going to be used by other people

446
00:16:44,670 --> 00:16:45,960
in their scripts too,

447
00:16:45,960 --> 00:16:48,150
and you can call from one script to another.

448
00:16:48,150 --> 00:16:50,640
And by being able to have that exit code come back,

449
00:16:50,640 --> 00:16:52,680
it can tell them the status of your script,

450
00:16:52,680 --> 00:16:54,480
that it was successful or not.

451
00:16:54,480 --> 00:16:56,670
Again, if you use exit code one,

452
00:16:56,670 --> 00:16:58,260
this can cause the script to terminate

453
00:16:58,260 --> 00:16:59,580
with a failure status.

454
00:16:59,580 --> 00:17:02,040
Whereas zero would say everything was satisfactory,

455
00:17:02,040 --> 00:17:03,450
keep on going.

456
00:17:03,450 --> 00:17:05,700
These exit codes are going to exist within your scripts,

457
00:17:05,700 --> 00:17:07,440
even if you don't define them,

458
00:17:07,440 --> 00:17:10,500
but by defining them and using proper exit codes,

459
00:17:10,500 --> 00:17:12,240
you can have multiple different errors

460
00:17:12,240 --> 00:17:13,470
that you can test for,

461
00:17:13,470 --> 00:17:15,089
and then you can give that information back

462
00:17:15,089 --> 00:17:16,530
to the script that's calling you.

463
00:17:16,530 --> 00:17:19,619
For example, maybe zero is going to be successful.

464
00:17:19,619 --> 00:17:21,359
One is going to be a failure.

465
00:17:21,359 --> 00:17:23,819
Two is going to be the fact that nobody gave you an argument

466
00:17:23,819 --> 00:17:25,560
to process in the first place.

467
00:17:25,560 --> 00:17:28,050
Things like that can be used as a way to give information

468
00:17:28,050 --> 00:17:30,935
from one script to another or one program to another.

469
00:17:30,935 --> 00:17:33,150
Now, another thing you want to take advantage of

470
00:17:33,150 --> 00:17:34,680
is the ability to do redirection

471
00:17:34,680 --> 00:17:37,170
and piping within the command line environment.

472
00:17:37,170 --> 00:17:39,330
You can do this by redirecting exit codes,

473
00:17:39,330 --> 00:17:41,520
either to the standard out, standard error

474
00:17:41,520 --> 00:17:43,950
or standard in, depending on what you want to do.

475
00:17:43,950 --> 00:17:46,770
And you can also redirect data to and from different files

476
00:17:46,770 --> 00:17:48,153
by using your redirections.

477
00:17:49,200 --> 00:17:50,370
Another thing to be aware of

478
00:17:50,370 --> 00:17:53,310
is how the bash shell actually interprets commands.

479
00:17:53,310 --> 00:17:56,130
And the way it does this is to split things up into tokens,

480
00:17:56,130 --> 00:17:57,720
also known as words.

481
00:17:57,720 --> 00:17:59,190
Shell expansion is the process

482
00:17:59,190 --> 00:18:01,830
by which the shell identifies these special tokens

483
00:18:01,830 --> 00:18:04,260
and then substitutes in values for them.

484
00:18:04,260 --> 00:18:07,110
Variable substitution is a type of shell expansion

485
00:18:07,110 --> 00:18:08,580
by which the shell going to identify

486
00:18:08,580 --> 00:18:10,440
that dollar sign special character,

487
00:18:10,440 --> 00:18:11,610
and then expand the value

488
00:18:11,610 --> 00:18:14,040
into its actual value that's being stored.

489
00:18:14,040 --> 00:18:14,873
In other words,

490
00:18:14,873 --> 00:18:17,490
when I do something like echo dollar sign variable,

491
00:18:17,490 --> 00:18:19,890
the echo command doesn't see the variable,

492
00:18:19,890 --> 00:18:22,050
instead it sees what the variable represents

493
00:18:22,050 --> 00:18:23,490
and what was stored inside of it

494
00:18:23,490 --> 00:18:25,050
because bash is going to expand it,

495
00:18:25,050 --> 00:18:27,432
before passing it over to the echo command.

496
00:18:27,432 --> 00:18:29,910
Now bash is going to perform its expressions

497
00:18:29,910 --> 00:18:31,380
in a defined order.

498
00:18:31,380 --> 00:18:33,810
Very similar to the order of mathematical operations

499
00:18:33,810 --> 00:18:35,490
you learned back in high school.

500
00:18:35,490 --> 00:18:36,600
The order of expansion

501
00:18:36,600 --> 00:18:39,450
is always going to start out with brace expansion first,

502
00:18:39,450 --> 00:18:41,250
then tilde expansion,

503
00:18:41,250 --> 00:18:43,470
then parameter and variable expansion,

504
00:18:43,470 --> 00:18:47,340
then arithmetic expansion and then command substitution,

505
00:18:47,340 --> 00:18:50,340
and then word splitting and then file name expansion.

506
00:18:50,340 --> 00:18:52,620
The shell will always perform things in that order,

507
00:18:52,620 --> 00:18:55,830
as it performs its expansions and substitutions.

508
00:18:55,830 --> 00:18:57,870
Now, when do you want to include a command's output

509
00:18:57,870 --> 00:18:59,700
within an existing string of text,

510
00:18:59,700 --> 00:19:02,100
command substitution can actually be used.

511
00:19:02,100 --> 00:19:03,870
This is a method where shell expansion

512
00:19:03,870 --> 00:19:05,370
allows the output of a command

513
00:19:05,370 --> 00:19:07,260
to replace the command itself.

514
00:19:07,260 --> 00:19:11,190
For example, if I enter the command, echo double quote,

515
00:19:11,190 --> 00:19:13,890
the current directory is backwards tick mark,

516
00:19:13,890 --> 00:19:16,500
pwd backwards tick mark, quote,

517
00:19:16,500 --> 00:19:19,320
this is going to actually replace the pwd

518
00:19:19,320 --> 00:19:22,410
and replace it with what the present working directory is.

519
00:19:22,410 --> 00:19:24,060
And this would output something like,

520
00:19:24,060 --> 00:19:26,220
the current directory is /root,

521
00:19:26,220 --> 00:19:28,410
if that was your present working directory.

522
00:19:28,410 --> 00:19:29,580
Now another way to do this

523
00:19:29,580 --> 00:19:32,010
is to use the format of dollar sign parenthesis,

524
00:19:32,010 --> 00:19:33,750
command, parenthesis.

525
00:19:33,750 --> 00:19:36,030
This will perform command substitution as well.

526
00:19:36,030 --> 00:19:39,600
For example, I could type this out by doing echo, quote,

527
00:19:39,600 --> 00:19:41,520
the current directory is dollar sign parenthesis,

528
00:19:41,520 --> 00:19:44,490
pwd, parenthesis, quote.

529
00:19:44,490 --> 00:19:46,710
And this would then do the exact same thing.

530
00:19:46,710 --> 00:19:49,680
Now, another thing we need to talk about is globbing.

531
00:19:49,680 --> 00:19:51,630
Now globbing is a bass shell feature

532
00:19:51,630 --> 00:19:52,680
that's used for matching

533
00:19:52,680 --> 00:19:54,774
or expanding specific types of patterns.

534
00:19:54,774 --> 00:19:57,210
Globbing is mainly used to match file names

535
00:19:57,210 --> 00:20:00,090
and searching for content within a given file.

536
00:20:00,090 --> 00:20:02,130
Globbing is going to rely on wild card characters

537
00:20:02,130 --> 00:20:03,540
to create different patterns,

538
00:20:03,540 --> 00:20:05,130
and that way you can find the information

539
00:20:05,130 --> 00:20:06,300
you're looking for.

540
00:20:06,300 --> 00:20:09,420
Now, there are three most commonly used wild card characters

541
00:20:09,420 --> 00:20:11,400
when you're creating a globbing pattern.

542
00:20:11,400 --> 00:20:13,440
These are the star, the question mark

543
00:20:13,440 --> 00:20:15,090
and the square brackets.

544
00:20:15,090 --> 00:20:18,270
First, we have the star also known as an asterisk.

545
00:20:18,270 --> 00:20:20,670
This is going to be used to match any number of characters.

546
00:20:20,670 --> 00:20:24,895
For example, if I type in cp star.txt

547
00:20:24,895 --> 00:20:27,600
tilde/destination,

548
00:20:27,600 --> 00:20:31,200
this is going to copy all the files with a .txt extension

549
00:20:31,200 --> 00:20:34,170
because that wild card is appearing before the period.

550
00:20:34,170 --> 00:20:38,708
And that means anything .txt is going to match my criteria.

551
00:20:38,708 --> 00:20:41,310
Now it's important to realize that when you're using a star

552
00:20:41,310 --> 00:20:44,460
or an asterisk, it's not replacing a single character,

553
00:20:44,460 --> 00:20:46,140
it's replacing zero characters,

554
00:20:46,140 --> 00:20:48,810
one characters or infinite number of characters.

555
00:20:48,810 --> 00:20:51,540
So if I had a file called .txt

556
00:20:51,540 --> 00:20:56,190
or a file called one.txt or a file called Jason.txt,

557
00:20:56,190 --> 00:20:58,260
all three of those will match the wildcard

558
00:20:58,260 --> 00:21:02,100
of star.txt because they all end with .txt,

559
00:21:02,100 --> 00:21:04,320
even if there's nothing before the dot.

560
00:21:04,320 --> 00:21:06,270
The second one we have is a question mark.

561
00:21:06,270 --> 00:21:09,540
Now the question mark is used to match a single character.

562
00:21:09,540 --> 00:21:12,300
For example, if I was looking for any file

563
00:21:12,300 --> 00:21:14,790
that had a single letter .txt,

564
00:21:14,790 --> 00:21:17,100
and I wanted to copy those to my destination directory,

565
00:21:17,100 --> 00:21:21,270
I could do that by typing in cp ?.txt

566
00:21:21,270 --> 00:21:23,340
tilde/destination.

567
00:21:23,340 --> 00:21:28,005
And this is going to copy files like a.txt, b.txt, c.txt,

568
00:21:28,005 --> 00:21:30,480
but it won't do Jason.txt

569
00:21:30,480 --> 00:21:32,370
because that's too many characters.

570
00:21:32,370 --> 00:21:35,040
Note here that if I do have that question mark,

571
00:21:35,040 --> 00:21:37,380
it can also represent a zero character.

572
00:21:37,380 --> 00:21:42,137
So .txt would still match just like a.txt and b.txt,

573
00:21:43,020 --> 00:21:46,365
but Jason will not because that's too many characters.

574
00:21:46,365 --> 00:21:49,470
The third type we have is notice square brackets.

575
00:21:49,470 --> 00:21:51,750
And any characters you put between the square brackets

576
00:21:51,750 --> 00:21:53,550
can be used to match any of the characters

577
00:21:53,550 --> 00:21:55,781
that are listed inside of there.

578
00:21:55,781 --> 00:21:59,716
For example, let's say I did something like cp bracket,

579
00:21:59,716 --> 00:22:04,716
ABC bracket .txt tilde/destination.

580
00:22:04,980 --> 00:22:06,150
What will this do?

581
00:22:06,150 --> 00:22:08,490
Well, it's going to copy all the files

582
00:22:08,490 --> 00:22:12,720
that are named a.txt, b.txt or c.txt.

583
00:22:12,720 --> 00:22:14,790
Notice inside those square brackets,

584
00:22:14,790 --> 00:22:16,470
we're only going to pick one letter

585
00:22:16,470 --> 00:22:19,380
out of the combinations there and be able to use those.

586
00:22:19,380 --> 00:22:22,620
So if I wanted to put all the letters like A through Z,

587
00:22:22,620 --> 00:22:25,200
I could do that by doing a dash Z

588
00:22:25,200 --> 00:22:26,820
inside those square brackets.

589
00:22:26,820 --> 00:22:29,370
And now I'm going to find every single file

590
00:22:29,370 --> 00:22:32,190
that starts with one of the letters .txt,

591
00:22:32,190 --> 00:22:34,800
which would be very similar to using the question mark.

592
00:22:34,800 --> 00:22:37,950
But if I wanted to be able to have just a, b and c,

593
00:22:37,950 --> 00:22:41,100
putting those inside my brackets will give me those options.

594
00:22:41,100 --> 00:22:42,690
Now, the next thing we're going to talk about

595
00:22:42,690 --> 00:22:44,370
is a positional parameter.

596
00:22:44,370 --> 00:22:45,510
Now positional parameter

597
00:22:45,510 --> 00:22:47,430
is a variable within your shell script

598
00:22:47,430 --> 00:22:50,160
that's assigned to an argument when the script is invoked.

599
00:22:50,160 --> 00:22:53,640
For example, let's say I call the script dion.sh

600
00:22:53,640 --> 00:22:58,380
by typing dion.sh arg one, arg two, arg three.

601
00:22:58,380 --> 00:23:01,680
Now the arg one argument corresponds to the first position

602
00:23:01,680 --> 00:23:04,050
which we refer to as dollar sign one.

603
00:23:04,050 --> 00:23:07,170
Arg two corresponds to the second position, dollar sign two,

604
00:23:07,170 --> 00:23:09,540
and arg three corresponds to the third position,

605
00:23:09,540 --> 00:23:11,610
dollar sign three, and so on.

606
00:23:11,610 --> 00:23:13,950
Notice there's a space between each of these arguments

607
00:23:13,950 --> 00:23:15,180
and that space is used

608
00:23:15,180 --> 00:23:17,400
to separate those positional parameters.

609
00:23:17,400 --> 00:23:19,800
So let's go ahead and look at dion.sh

610
00:23:19,800 --> 00:23:21,330
this little script I have.

611
00:23:21,330 --> 00:23:22,557
I have four lines.

612
00:23:22,557 --> 00:23:25,530
The first line defines that this is a bash script

613
00:23:25,530 --> 00:23:28,800
by doing hashtag exclamation slash bin slash bash.

614
00:23:28,800 --> 00:23:30,690
And then the next three are all looking the same,

615
00:23:30,690 --> 00:23:33,090
but the only difference is which argument we're calling.

616
00:23:33,090 --> 00:23:35,220
Echo dollar sign one is the first argument,

617
00:23:35,220 --> 00:23:36,987
echo dollar sign two is the second argument

618
00:23:36,987 --> 00:23:39,810
and echo dollar sign three is the third argument.

619
00:23:39,810 --> 00:23:43,260
Now, if I call this script by entering dion.sh,

620
00:23:43,260 --> 00:23:46,560
space, Jason space, dion space, training,

621
00:23:46,560 --> 00:23:48,030
what am I going to get back?

622
00:23:48,030 --> 00:23:50,670
Well, I'm going to get three sentences back that say,

623
00:23:50,670 --> 00:23:52,140
Jason is the first argument,

624
00:23:52,140 --> 00:23:53,550
Dion is the second argument

625
00:23:53,550 --> 00:23:55,500
and training is the third argument.

626
00:23:55,500 --> 00:23:57,090
Now this is a really simple script,

627
00:23:57,090 --> 00:23:59,100
and all it's doing is echoing out information

628
00:23:59,100 --> 00:24:01,020
that we gave it from the command prompt.

629
00:24:01,020 --> 00:24:03,930
Now, what happens if I forget to give any arguments?

630
00:24:03,930 --> 00:24:06,293
Well, let's say I just run it as dion.sh,

631
00:24:06,293 --> 00:24:07,680
what's going to happen?

632
00:24:07,680 --> 00:24:09,690
Well, it's going to return the three sentences,

633
00:24:09,690 --> 00:24:12,930
but the arguments are being filled in with a blank space

634
00:24:12,930 --> 00:24:14,910
because we didn't give it any arguments.

635
00:24:14,910 --> 00:24:17,910
So we're going to get blank as the first argument,

636
00:24:17,910 --> 00:24:19,260
blank is the second argument

637
00:24:19,260 --> 00:24:21,963
and blank is the third argument displayed on our screen.

638
00:24:21,963 --> 00:24:24,600
Now, if you want to hard code the arguments into the script,

639
00:24:24,600 --> 00:24:27,570
you can do that by using something known as set.

640
00:24:27,570 --> 00:24:30,060
So we're going to modify our script by adding a new line

641
00:24:30,060 --> 00:24:34,350
that says set space dash dash space, CompTIA space,

642
00:24:34,350 --> 00:24:36,120
Dion space, training.

643
00:24:36,120 --> 00:24:38,850
Now because I've hardcoded those arguments into the script,

644
00:24:38,850 --> 00:24:41,340
I don't have to enter them at the command line.

645
00:24:41,340 --> 00:24:44,850
So I simply enter dion.sh and hit enter,

646
00:24:44,850 --> 00:24:46,920
and these three sentences are going to be returned

647
00:24:46,920 --> 00:24:49,290
to the screen with those arguments filled in.

648
00:24:49,290 --> 00:24:52,440
So now I get CompTIA is the first argument,

649
00:24:52,440 --> 00:24:54,150
Dion is the second argument,

650
00:24:54,150 --> 00:24:56,850
training is the third argument and so on.

651
00:24:56,850 --> 00:24:59,370
Now note because I hardcoded those into the script

652
00:24:59,370 --> 00:25:00,870
using the set command,

653
00:25:00,870 --> 00:25:03,720
I can no longer pass those arguments from the command line

654
00:25:03,720 --> 00:25:04,890
and overwrite them.

655
00:25:04,890 --> 00:25:07,200
So if I entered in the command I used before

656
00:25:07,200 --> 00:25:11,190
of dion.sh space, Jason space, Dion space, training,

657
00:25:11,190 --> 00:25:13,710
I'm still going to get the same three sentences out

658
00:25:13,710 --> 00:25:16,260
that I got before with those hard coded arguments,

659
00:25:16,260 --> 00:25:19,230
because it's going to say CompTIA is the first argument,

660
00:25:19,230 --> 00:25:21,240
not Jason is the first argument

661
00:25:21,240 --> 00:25:23,907
because CompTIA is hard coded inside of the script,

662
00:25:23,907 --> 00:25:25,410
and so we're going to ignore the arguments

663
00:25:25,410 --> 00:25:27,300
passed from the command line.

664
00:25:27,300 --> 00:25:30,000
Now, the next command we're going to talk about is exec.

665
00:25:30,000 --> 00:25:32,850
Now the exec command does not create a new process

666
00:25:32,850 --> 00:25:33,683
when you run it.

667
00:25:33,683 --> 00:25:36,240
Instead, it only replaces the bash shell

668
00:25:36,240 --> 00:25:38,400
with the command being executed.

669
00:25:38,400 --> 00:25:40,440
Now, this is useful if you want to prevent the user

670
00:25:40,440 --> 00:25:43,080
from returning to a parent process inside your script,

671
00:25:43,080 --> 00:25:44,880
when there's an error that's encountered.

672
00:25:44,880 --> 00:25:47,610
For example, you might want to terminate a privileged shell

673
00:25:47,610 --> 00:25:48,990
if a command fails.

674
00:25:48,990 --> 00:25:50,520
And if no command is supplied,

675
00:25:50,520 --> 00:25:51,870
the redirections are going to be used

676
00:25:51,870 --> 00:25:54,060
to modify the current shell environment.

677
00:25:54,060 --> 00:25:55,410
This can be very useful to allow us

678
00:25:55,410 --> 00:25:57,480
to change the file descriptors of a shell

679
00:25:57,480 --> 00:25:58,980
based on our desires.

680
00:25:58,980 --> 00:26:01,860
The process is going to continue even after the exec command,

681
00:26:01,860 --> 00:26:03,390
unlike the previous case,

682
00:26:03,390 --> 00:26:05,490
but now standard input, output and error

683
00:26:05,490 --> 00:26:08,790
are modified according to the redirections that we have.

684
00:26:08,790 --> 00:26:11,130
Another command we have to know about is source.

685
00:26:11,130 --> 00:26:13,650
The source command is used to execute another command

686
00:26:13,650 --> 00:26:15,810
within the current shell process.

687
00:26:15,810 --> 00:26:18,180
In this sense, it really performs the opposite function

688
00:26:18,180 --> 00:26:19,770
of the exec command.

689
00:26:19,770 --> 00:26:21,120
This is really useful when you'd like

690
00:26:21,120 --> 00:26:22,560
to stay within your current shell

691
00:26:22,560 --> 00:26:24,240
when you're executing a script.

692
00:26:24,240 --> 00:26:26,760
For example, let's say you wanted to change a directory

693
00:26:26,760 --> 00:26:28,110
inside of your script.

694
00:26:28,110 --> 00:26:30,420
Well, you'd probably want to source that first

695
00:26:30,420 --> 00:26:33,270
and by sourcing it, it means that after the script executes,

696
00:26:33,270 --> 00:26:35,850
your location will be whatever directory you change to

697
00:26:35,850 --> 00:26:37,110
inside of the script,

698
00:26:37,110 --> 00:26:38,640
because the script was operating

699
00:26:38,640 --> 00:26:41,250
just as if you were typing in those commands.

700
00:26:41,250 --> 00:26:42,660
Now for the most part,

701
00:26:42,660 --> 00:26:44,910
Linux file extensions are really optional.

702
00:26:44,910 --> 00:26:46,290
You don't need them on everything,

703
00:26:46,290 --> 00:26:48,600
but you'll notice I use .sh

704
00:26:48,600 --> 00:26:51,120
when I gave an extension to my bash script.

705
00:26:51,120 --> 00:26:52,200
Why is that?

706
00:26:52,200 --> 00:26:55,170
Well, it's because Linux checks the files metadata

707
00:26:55,170 --> 00:26:56,880
to determine what type of file it is.

708
00:26:56,880 --> 00:27:01,109
But those .shs and .txts, that helps us as humans

709
00:27:01,109 --> 00:27:03,450
know what type of a file we're looking at.

710
00:27:03,450 --> 00:27:05,430
So you don't really need to name your scripts

711
00:27:05,430 --> 00:27:07,860
with an extension, but by default,

712
00:27:07,860 --> 00:27:11,580
we do add .sh as an extension for shells scripts

713
00:27:11,580 --> 00:27:13,740
to ensure that everybody understands that that is a script

714
00:27:13,740 --> 00:27:15,510
and it has special meaning to us.

715
00:27:15,510 --> 00:27:17,970
It really is for the people and not for the computer,

716
00:27:17,970 --> 00:27:19,800
which is something to be aware of.

717
00:27:19,800 --> 00:27:22,230
Remember your ability to use any file,

718
00:27:22,230 --> 00:27:24,600
including a script is really only going to be constrained

719
00:27:24,600 --> 00:27:26,490
by the permissions assigned to that script

720
00:27:26,490 --> 00:27:27,870
and your user account.

721
00:27:27,870 --> 00:27:29,460
So even though you created a script,

722
00:27:29,460 --> 00:27:32,070
you're not going to automatically have permissions to run that

723
00:27:32,070 --> 00:27:34,140
unless you do a change mod command

724
00:27:34,140 --> 00:27:36,180
and you give it execute abilities.

725
00:27:36,180 --> 00:27:38,460
So make sure you set two permissions

726
00:27:38,460 --> 00:27:40,050
every time you write a script.

727
00:27:40,050 --> 00:27:42,120
First, you need to ensure that your user

728
00:27:42,120 --> 00:27:44,850
has the permissions to execute the script itself.

729
00:27:44,850 --> 00:27:47,220
And second, you want to make sure that you have right

730
00:27:47,220 --> 00:27:49,320
and execute bits set on the directory

731
00:27:49,320 --> 00:27:50,850
that contains that script.

732
00:27:50,850 --> 00:27:53,490
If that directory doesn't have the execute bits set,

733
00:27:53,490 --> 00:27:55,530
it's not going to allow you to be able to run that script,

734
00:27:55,530 --> 00:27:57,930
even if the script is set to execute.

735
00:27:57,930 --> 00:28:00,750
Remember, you can always control who can access the files,

736
00:28:00,750 --> 00:28:02,730
search their directories and run scripts

737
00:28:02,730 --> 00:28:04,890
using Linux's change mod command

738
00:28:04,890 --> 00:28:07,830
by being able to apply or remove, read, write,

739
00:28:07,830 --> 00:28:10,773
and execute bits onto those directories and files.

