1
00:00:03,100 --> 00:00:07,810
In this lesson you're going to learn some very important details when it comes to getting started with

2
00:00:07,810 --> 00:00:08,940
shell scripting.

3
00:00:09,040 --> 00:00:15,520
You're going to learn about shebang comments shell built ins variables and more so there's really a

4
00:00:15,520 --> 00:00:17,280
lot in this first lesson.

5
00:00:17,320 --> 00:00:21,850
If you have shell scripting experience in those topics seem basic are old hat to you.

6
00:00:21,850 --> 00:00:24,680
Just be patient with me bear with me for just a moment.

7
00:00:24,730 --> 00:00:28,080
We'll be getting to some new material for you soon enough.

8
00:00:28,150 --> 00:00:32,590
You can either follow along right now with me or wait until the practice exercise.

9
00:00:32,590 --> 00:00:37,810
That's coming up very soon in the practice exercise you'll get to apply the concepts that you learn

10
00:00:38,170 --> 00:00:39,910
in this lesson.

11
00:00:40,030 --> 00:00:46,330
The first thing I'm going to do is open up a terminal on my local machine here I am on my Mac and this

12
00:00:46,330 --> 00:00:51,970
process is the same whether you're using Mac or Linux or Windows as your primary OS.

13
00:00:52,000 --> 00:00:56,440
Now what I want to do is move into our working directory which is shall class.

14
00:00:56,530 --> 00:01:02,980
So I'll S.D. into shell class and now I'm going to make a new directory that's going to house our vagrant

15
00:01:02,980 --> 00:01:08,770
project for a virtual machine that we're going to create and work on for the next few sessions and for

16
00:01:08,770 --> 00:01:10,720
the first couple of practice exercises.

17
00:01:11,260 --> 00:01:18,270
So we'll get into the show class director here we'll create local users make sure local users will change

18
00:01:18,270 --> 00:01:19,020
into that directory.

19
00:01:20,910 --> 00:01:26,430
The reason why I'm calling this directory local users is that we're building toward creating a script

20
00:01:26,790 --> 00:01:31,950
that is going to create local users or users that are local to a system.

21
00:01:31,950 --> 00:01:38,040
Later on in the course we'll be creating a script that will add remote users or users to systems other

22
00:01:38,040 --> 00:01:40,560
than the one that your script is executing on.

23
00:01:40,590 --> 00:01:44,220
But for now we're focusing on local users.

24
00:01:44,220 --> 00:01:49,830
Now let's create the vagrant file with vagrant Annette and we'll specify the class image that we're

25
00:01:49,830 --> 00:01:50,130
using

26
00:01:53,490 --> 00:01:58,170
in order to run and execute the shell scripts that you and I are going to be creating we need to be

27
00:01:58,170 --> 00:02:02,620
inside the virtual machine in side the Linux system that we've created.

28
00:02:02,730 --> 00:02:09,600
So we'll get in there by connecting over SSA to a run vagrant SS age and that will connect us to the

29
00:02:09,600 --> 00:02:11,220
virtual machine that we just created.

30
00:02:12,030 --> 00:02:18,300
Let's change into the forward slash vagrant rectory with seedy forward slash vagrant and press enter.

31
00:02:18,300 --> 00:02:25,920
I'm going to run LSA dash l here and that is a long listing format of the Ellis command the dash L and

32
00:02:25,920 --> 00:02:28,560
it just displays the files that are in the directory.

33
00:02:28,560 --> 00:02:35,880
Now it's important to note that we have the vagrant file so on our current system in forward slash vagrant.

34
00:02:36,000 --> 00:02:38,430
It's the same as our local system.

35
00:02:38,430 --> 00:02:41,820
As in your home directory shall class local users.

36
00:02:41,820 --> 00:02:44,530
So as you can see here this is my home directory.

37
00:02:44,670 --> 00:02:49,200
It mounted slash vagrant to this local system.

38
00:02:49,200 --> 00:02:55,590
So whatever we create inside the VM and the vagrant directory ends up on our local machine in that local

39
00:02:55,650 --> 00:02:58,500
user's directory and vice versa.

40
00:02:58,500 --> 00:03:00,570
So here you really have a choice to make.

41
00:03:00,570 --> 00:03:07,890
You can either edit your scripts inside the virtual machine using nano emacs or vim or you can use the

42
00:03:07,890 --> 00:03:09,540
editor on your local system.

43
00:03:09,900 --> 00:03:13,190
So for me I'm going to use vim because that's my favorite editor.

44
00:03:13,190 --> 00:03:14,790
I've done a class on it.

45
00:03:14,880 --> 00:03:17,010
I could talk hours and hours about them.

46
00:03:17,010 --> 00:03:17,900
I totally love it.

47
00:03:17,910 --> 00:03:19,640
So that's what I'm going to do.

48
00:03:19,650 --> 00:03:24,850
But I will demonstrate what it looks like from the other side here so I'm going to go ahead and start

49
00:03:24,850 --> 00:03:25,770
creating the script.

50
00:03:25,780 --> 00:03:35,710
I'm going to use my vim command vim and I'm going to name the script L user dash demo 0 one dot S H.

51
00:03:35,710 --> 00:03:40,930
Now I want to point out here that file names with shell scripts really don't matter.

52
00:03:40,930 --> 00:03:42,990
We'll get into what does matter in just a second.

53
00:03:43,000 --> 00:03:49,950
So by convention shell scripts can end in dot s h but they're not significant.

54
00:03:49,960 --> 00:03:56,290
The file extension is not significant like it is for example on windows when something has a dot exec

55
00:03:56,290 --> 00:03:57,290
file extension.

56
00:03:57,430 --> 00:03:59,130
That's not the case on Linux.

57
00:03:59,170 --> 00:04:06,880
We could name this anything we could just name it our user test demo or one or DSH or Bob or Fred or

58
00:04:06,940 --> 00:04:10,720
any name that is a valid file name and that will work as a shell script.

59
00:04:10,720 --> 00:04:15,970
So let me edit this file real quick and I'm just going to put a little bit of information in here.

60
00:04:16,120 --> 00:04:25,240
A pound sign an exclamation mark Ford slash been 4 slash bash and I'm going to save my changes here

61
00:04:26,080 --> 00:04:31,240
and what I'm going to do to show you what happens with the file sharing I'm just going to do an Alice

62
00:04:31,240 --> 00:04:31,900
in this directory.

63
00:04:31,900 --> 00:04:35,160
You can see that the El user Desdemona Wanda s each file was created.

64
00:04:35,260 --> 00:04:40,480
But I'm going to open up that file in an editor on my local MAC system and so I'm going to use Adam

65
00:04:40,630 --> 00:04:41,290
in my case.

66
00:04:41,980 --> 00:04:47,850
So here I have that same file open in the Adam Ed but as you can see it's in the shell class.

67
00:04:47,860 --> 00:04:49,180
Local users.

68
00:04:49,240 --> 00:04:50,050
So that's where it's at.

69
00:04:50,050 --> 00:04:51,320
On your local system.

70
00:04:51,460 --> 00:04:53,320
I'm just gonna add another line of text here.

71
00:04:53,320 --> 00:05:02,170
I'll say hello from the main O S and I'm going to save my changes now if I go back to the terminal where

72
00:05:02,170 --> 00:05:06,310
I'm logged into the virtual machine and look at this file those changes will be there.

73
00:05:06,340 --> 00:05:10,700
Let me go ahead and just close up this file to make sure nothing is going to interfere with that will

74
00:05:10,720 --> 00:05:16,720
go back to our terminal and if we display the contents of that file El user demo one that S H.

75
00:05:16,870 --> 00:05:22,660
You can see that this new line of text has been added so that's what I'm talking about your decision

76
00:05:22,660 --> 00:05:27,790
of whether to edit from your local machine or whether to edit inside the virtual machine.

77
00:05:27,790 --> 00:05:33,730
Again I'm going to primarily edit inside the virtual machine here and I'm going to get back into that

78
00:05:33,730 --> 00:05:41,290
file vim El user dash demo 0 1 and by the way El user is just my personal shorthand for local user.

79
00:05:41,290 --> 00:05:42,910
So that's what that's about.

80
00:05:42,910 --> 00:05:46,180
Now let's talk about this very first line in this shell script.

81
00:05:46,330 --> 00:05:53,620
It's a pound sign followed by an exclamation point and then a path to bash which is forward slash Ben

82
00:05:53,950 --> 00:05:55,560
or slash bash.

83
00:05:55,600 --> 00:05:57,940
This is called a shebang.

84
00:05:57,970 --> 00:06:03,370
The reason why it's called a shebang is the pound sign is sometimes called a sharp.

85
00:06:03,370 --> 00:06:09,670
If you've ever played music or read music you'll know that the pound sign looks very very similar to

86
00:06:09,670 --> 00:06:10,660
a sharp sign of music.

87
00:06:10,660 --> 00:06:14,870
So a lot of people call the hash mark or hash symbol or pound sign.

88
00:06:14,890 --> 00:06:17,470
Sharp as far as the exclamation point.

89
00:06:17,470 --> 00:06:19,740
Slang for that is bang.

90
00:06:19,810 --> 00:06:25,720
So we have sharp bang but an inexact contraction of those two words is shebang.

91
00:06:25,990 --> 00:06:27,910
So that's where shebang comes from.

92
00:06:27,910 --> 00:06:30,340
And you want to start out each shell script with the shebang.

93
00:06:30,340 --> 00:06:33,160
Sharp bang and then a path to the interpreter.

94
00:06:33,160 --> 00:06:38,980
So what will happen when you execute this script is that the commands inside the script will be interpreted

95
00:06:39,160 --> 00:06:42,100
by 4D slash band forward slash Bash.

96
00:06:42,100 --> 00:06:54,280
Now if you had put in another interpreter for example let's say user bean Python or band Ruby or whatever.

97
00:06:54,280 --> 00:06:58,240
Now that's a ruby script but we're doing shell so we'll keep it as bash.

98
00:06:58,270 --> 00:07:05,970
So really what happens is when you execute this script Ben Bash we'll get executed and the file name

99
00:07:06,570 --> 00:07:14,250
actually gets passed to that script and what happens is bash then executes everything in this file.

100
00:07:14,310 --> 00:07:18,180
I'll demonstrate that later in the course by showing the process table and so on.

101
00:07:18,240 --> 00:07:23,820
But for now just know that the first line the shebang determines what is going to execute the commands

102
00:07:23,880 --> 00:07:26,010
listed in the file that you're creating.

103
00:07:26,010 --> 00:07:31,230
If you do not supply a shebang and specify an interpreter on the first line of the script the commands

104
00:07:31,290 --> 00:07:34,650
in the script will be executed using your current shell.

105
00:07:34,890 --> 00:07:37,150
And that may not be what you want.

106
00:07:37,170 --> 00:07:43,110
For example if you have some bash specific items in your shell script and you happen to be using the

107
00:07:43,170 --> 00:07:48,250
CSA each shell when you execute it then you're gonna end up with some errors.

108
00:07:48,360 --> 00:07:52,200
And this is really important especially if you're working with the team you're writing a shell script

109
00:07:52,410 --> 00:07:56,960
and you have no idea what Shell the other person will be using when they execute this.

110
00:07:56,970 --> 00:08:02,520
So you want to be very explicit use the shebang and make sure you specify which interpreter that you

111
00:08:02,520 --> 00:08:04,840
want to use for the script.

112
00:08:04,860 --> 00:08:10,770
So I want to remove this line we added in the other editor and what I like to do at the top of my shell

113
00:08:10,770 --> 00:08:18,590
scripts is to give a goal or comment about what's going to happen when the shell script is executed.

114
00:08:18,600 --> 00:08:22,650
So when you start a line with a pound sign it's actually a comment.

115
00:08:22,650 --> 00:08:24,960
Comments are not executed.

116
00:08:24,960 --> 00:08:29,790
They're just simply there for us humans only with the exception of course of the first line which is

117
00:08:29,790 --> 00:08:30,680
a special line.

118
00:08:30,780 --> 00:08:35,510
But in all other cases that information there is just for us humans.

119
00:08:35,610 --> 00:08:41,130
So we can put anything after the pound sign after the comment and it's not going to affect the functioning

120
00:08:41,130 --> 00:08:42,360
of our program.

121
00:08:42,390 --> 00:08:45,020
And so here at the very top of the script I'm going to tell what it does

122
00:08:52,890 --> 00:08:56,120
so this script just displays various information to the screen.

123
00:08:56,130 --> 00:08:57,310
That's all this script does.

124
00:08:57,360 --> 00:08:59,570
And so that's what I'm going to put at the top of the file.

125
00:08:59,580 --> 00:09:04,710
Now when you start making very long and complex scripts and you come back to it a year later or someone

126
00:09:04,710 --> 00:09:06,640
else looks at it and they're not sure what it does.

127
00:09:06,660 --> 00:09:10,980
Just having a few notes at the top of the script can really help someone out there.

128
00:09:10,980 --> 00:09:12,760
I'm going to add another comment.

129
00:09:12,810 --> 00:09:17,930
We're going to say what we want to do is display the text.

130
00:09:18,000 --> 00:09:24,010
Hello and we're going to use a command called Echo and then supply the text we want to display to the

131
00:09:24,010 --> 00:09:28,300
screen which is helo and we'll put that in single quotes.

132
00:09:28,300 --> 00:09:34,310
I'm going to save my changes and then I'm going to exit out of the editor.

133
00:09:34,340 --> 00:09:38,180
So just a minute ago I talked about file extensions and how they weren't relevant.

134
00:09:38,200 --> 00:09:42,530
Now what is relevant is an execute bet on the script.

135
00:09:42,610 --> 00:09:47,290
Now hopefully by the time you've got to this course you understand file permissions but very quickly

136
00:09:47,290 --> 00:09:49,800
I'm going to do a quick review of them.

137
00:09:49,930 --> 00:09:54,000
We'll just take a look at our previous LSA output here as a demonstration.

138
00:09:54,010 --> 00:10:03,760
So these are w characters are stands for read W stands for right and what goes here is X for executable

139
00:10:04,650 --> 00:10:08,410
and there will be three sets of these three permissions.

140
00:10:08,410 --> 00:10:11,770
So we have RW dash our dash dash dash dash.

141
00:10:11,800 --> 00:10:19,130
So the first three characters here represents the permissions of the owner of the file.

142
00:10:19,240 --> 00:10:24,850
The next three characters represents the permissions that are granted to the group in the file.

143
00:10:25,000 --> 00:10:30,460
In the last three characters here represent the permissions that everyone else on the system has to

144
00:10:30,460 --> 00:10:31,090
this file.

145
00:10:31,930 --> 00:10:35,950
So in this case the vagrant user.

146
00:10:35,950 --> 00:10:39,610
This is the user as read write permissions.

147
00:10:39,880 --> 00:10:46,030
The vagrant group has read permissions and then everyone else who is not the vagrant user or in the

148
00:10:46,030 --> 00:10:50,410
vagrant group has read permissions to this file.

149
00:10:50,440 --> 00:10:56,320
What we want to do is add execute permissions so that I you and anyone else can actually execute this

150
00:10:56,320 --> 00:10:58,790
file and how we do that is with command.

151
00:10:58,810 --> 00:11:09,040
So due to mod 7 5 5 0 user demo or 1 And now let's look at the permissions now they are RW X which is

152
00:11:09,040 --> 00:11:17,110
read write execute read execute for the group read execute for everyone else that's not user or the

153
00:11:17,110 --> 00:11:19,800
owner of the file and in the vagrant group.

154
00:11:19,810 --> 00:11:24,610
So what this does is if you have read permission obviously you can look at the contents of the file

155
00:11:24,880 --> 00:11:30,040
if you have right permission that means you can change the contents of the file and if you have X permission

156
00:11:30,040 --> 00:11:32,290
that means you can execute the file.

157
00:11:32,290 --> 00:11:37,760
Typically we want to rewrite and execute for ourselves and read and execute for other people.

158
00:11:37,840 --> 00:11:42,790
By the way if you can't read the file then you can execute it because you can't see the contents that

159
00:11:42,790 --> 00:11:46,000
are going to be read in by the interpreter and execute it and so on.

160
00:11:46,000 --> 00:11:51,090
So typically the default permission you want to put on your shell scripts is 7 5 5.

161
00:11:51,100 --> 00:11:54,400
So really quickly review where does this 7 5 5 come from.

162
00:11:54,640 --> 00:12:03,580
Well read you can think of as 4 right as 2 and execute as one and you add those together.

163
00:12:03,580 --> 00:12:11,800
So 7 is four plus two plus one which is read plus write posts execute so 4 to one is 7 and that is for

164
00:12:11,800 --> 00:12:16,560
the owner of the file 5 is read which is 4 plus 1 execute.

165
00:12:16,570 --> 00:12:17,580
So that's 5.

166
00:12:17,770 --> 00:12:19,390
And that is for the group.

167
00:12:19,660 --> 00:12:22,800
So this corresponds to this middle set here.

168
00:12:22,810 --> 00:12:29,050
5 again is the same thing 4 plus 1 is 5 and that is for everyone else.

169
00:12:29,050 --> 00:12:33,430
So if this is a little bit confusing for you or you don't want to remember the specifics.

170
00:12:33,430 --> 00:12:38,560
Just remember that the default permission we're going to be using is to mod seven fifty five on the

171
00:12:38,560 --> 00:12:40,190
name of your script.

172
00:12:40,210 --> 00:12:40,400
Okay.

173
00:12:40,420 --> 00:12:41,470
So now we have a script.

174
00:12:41,500 --> 00:12:42,270
It's executed.

175
00:12:42,280 --> 00:12:44,360
Also how do we execute that script.

176
00:12:44,710 --> 00:12:50,770
Well the first thing you want to do is type in period forward slash and the name of the script and then

177
00:12:50,770 --> 00:12:52,090
press enter.

178
00:12:52,270 --> 00:12:55,370
So the output of this script is simply the word hello.

179
00:12:55,480 --> 00:13:00,430
And that is because of the command echo Hello that we placed in the script.

180
00:13:00,520 --> 00:13:05,710
Now I want to point out here that dot represents this directory.

181
00:13:05,710 --> 00:13:12,340
So for example if we did seed space dot we wouldn't go anywhere we would still be in the same directory.

182
00:13:12,340 --> 00:13:14,470
Dot dot represents the parent directory.

183
00:13:14,500 --> 00:13:19,240
So if we did see dot dot we move to the directory above the directory we were in.

184
00:13:19,240 --> 00:13:24,330
Let me go back in the vagrant directory so dot is this directory.

185
00:13:24,340 --> 00:13:32,950
And as you can see the forward slash is the directory separator so dot forward slash means this directory

186
00:13:33,040 --> 00:13:38,240
is dot forward slash is a directory separator and then the name of the script is the file.

187
00:13:38,380 --> 00:13:41,050
So that's how you execute a file in your current directory.

188
00:13:41,050 --> 00:13:43,860
Dot forward slash and then the name of the file.

189
00:13:43,960 --> 00:13:46,590
This is equivalent to the full path.

190
00:13:46,600 --> 00:13:53,550
So we could actually run or slash vagrant l user demo 0 1 and hit enter as well.

191
00:13:53,650 --> 00:13:58,000
And again the reason why this dot slash works is because dot represents the current directory which

192
00:13:58,000 --> 00:14:00,140
is a Ford slash vagrant.

193
00:14:00,520 --> 00:14:06,250
And then we specify a Ford slash which is the same as this Ford slash and then the name of the file

194
00:14:06,280 --> 00:14:08,200
which is the same as the name of this file.

195
00:14:08,740 --> 00:14:11,050
So that's why that works so that's how you do it.

196
00:14:11,080 --> 00:14:16,570
So just remember tomorrow some 55 dot Ford slash and then the name of the file by the way we could have

197
00:14:16,570 --> 00:14:22,260
named this file anything again dot DSH doesn't matter as a matter of fact I'm going to rename the file

198
00:14:22,270 --> 00:14:28,480
really quick l user demo I want to Jason it's a matter of fact that's a typo I even misspelled my own

199
00:14:28,480 --> 00:14:34,780
name incorrectly but it doesn't matter I'm an executed anyway dot org slash J a o s n and we get the

200
00:14:34,780 --> 00:14:36,490
same results I'm going to move that back

201
00:14:42,180 --> 00:14:46,890
let me show you what it looks like when you don't have the execute bit set let's just use the touch

202
00:14:46,890 --> 00:14:53,970
command to create an empty file that's what touch does touch either creates an empty file if one doesn't

203
00:14:53,970 --> 00:15:00,410
exist or it updates the lacks access timestamp of a file do an Alice here in this directory.

204
00:15:00,480 --> 00:15:08,100
So we have blood out S H There is no executable bit set on it so if we do dot forward slash Blau S H

205
00:15:08,100 --> 00:15:15,360
and hit enter we get permission denied because we do not have execute permission on that file so that

206
00:15:15,360 --> 00:15:19,170
is what's gonna happen when you don't have the proper permissions set so again make sure you do trim

207
00:15:19,170 --> 00:15:24,120
on seven fifty five in your file and you'll be good to go let's continue working on our script we'll

208
00:15:24,120 --> 00:15:27,060
use vim and edited our file here.

209
00:15:27,420 --> 00:15:32,490
So let's look at this echo command I kind of glossed over it to just get it started here and show the

210
00:15:32,490 --> 00:15:36,390
execute permissions and talk about file extensions and that sort of thing.

211
00:15:36,480 --> 00:15:40,140
So the echo command just displays whatever is passed to it.

212
00:15:40,620 --> 00:15:42,090
Well where does echo come from.

213
00:15:42,090 --> 00:15:49,120
Well let's check it out back on the command line Echo is actually a shell built in.

214
00:15:49,120 --> 00:15:54,490
That means it's a command that's built within the shell it doesn't require any external programs to

215
00:15:54,490 --> 00:15:57,400
execute it's really just part of bash.

216
00:15:57,400 --> 00:15:59,230
So how do we know it's a shell built in.

217
00:15:59,230 --> 00:16:04,570
Well you can use that type command which is also a shell built in to show you if it's a shell built

218
00:16:04,570 --> 00:16:04,980
in or not.

219
00:16:04,980 --> 00:16:10,930
So if you do type space the command you're interested about which we are interested in echo it says

220
00:16:11,020 --> 00:16:12,730
Echo is a shell built in.

221
00:16:12,730 --> 00:16:19,960
So if you want to see all instances of Echo on your system you do type dash a for all and then type

222
00:16:19,960 --> 00:16:20,670
echo.

223
00:16:20,800 --> 00:16:23,320
And so it listen in the order that they will execute.

224
00:16:23,350 --> 00:16:24,490
Echo is a shell built in.

225
00:16:24,490 --> 00:16:29,110
So really that's what's going to execute when you type echo we'll just type echo now on the command

226
00:16:29,110 --> 00:16:30,610
line echo Hello.

227
00:16:30,610 --> 00:16:36,510
Just like we had on our script and press enter and the shell built in is what is executed.

228
00:16:36,700 --> 00:16:43,480
Now we can force the execution of this by using the full path user bean echo and then pass something

229
00:16:43,480 --> 00:16:49,930
to it to display what you want to do is actually anytime there is a shell built in to use the shell

230
00:16:49,930 --> 00:16:50,710
built in.

231
00:16:50,710 --> 00:16:53,030
So don't specify the full path.

232
00:16:53,050 --> 00:16:58,240
Just use echo and it's a little more efficient because again it's built into the shell the shell doesn't

233
00:16:58,240 --> 00:17:02,430
have to request an external program to do some extra processing and what have you.

234
00:17:02,440 --> 00:17:05,810
So just use the shell built in when it's available.

235
00:17:05,830 --> 00:17:12,460
Another advantage of using shell built ins is that it's a more portable for example if the echo command

236
00:17:12,460 --> 00:17:17,770
is on Ben echo and some systems and it's on user been in another system then you might run into some

237
00:17:17,770 --> 00:17:18,600
issues there.

238
00:17:18,700 --> 00:17:23,950
But if you use the shell built in you avoid all of that so you can kind of think of these built ins

239
00:17:23,980 --> 00:17:29,870
as free stuff you get by using bash and there's more than just these built in commands and functions.

240
00:17:30,040 --> 00:17:34,210
There are built in variables and all sorts of things which we'll be getting into soon enough.

241
00:17:34,510 --> 00:17:41,250
By the way if you want to get help on a shell built in use the help command so we'll do help echo and

242
00:17:41,250 --> 00:17:46,510
a lot of that information scrolled off our screen quickly someone to pipe it to less it's a pager so

243
00:17:46,510 --> 00:17:52,300
we can page up and down to this output I'm going to pipe it to less it enter and it says echo rights

244
00:17:52,330 --> 00:17:58,510
arguments to standard output display the arguments on the standard output followed by a new line.

245
00:17:58,510 --> 00:18:01,150
So that's what the echo built in does.

246
00:18:01,180 --> 00:18:06,460
Let's look at an example of a command that is not a shell built in one command that comes to mind as

247
00:18:06,520 --> 00:18:13,210
uptime so uptime just displays how long this system that you're logged into has been up will do type

248
00:18:13,270 --> 00:18:15,950
dash a pass and uptime to that.

249
00:18:16,050 --> 00:18:21,700
This is uptime is actually user been uptime that's what it gets executed when you run uptime.

250
00:18:21,700 --> 00:18:27,410
So if you run help uptime you're not gonna get any help because it's not a shell built in.

251
00:18:27,430 --> 00:18:32,980
However if you wanted to get help on uptime you would use the man command which is short for man pages.

252
00:18:33,000 --> 00:18:36,950
So man or manual man uptime and press enter.

253
00:18:37,030 --> 00:18:42,310
So that's how you get information on how to use commands that are not part of shell built ends and we'll

254
00:18:42,310 --> 00:18:47,250
be looking at man pages along the way as well as help or built in Shell commands.

255
00:18:47,470 --> 00:18:53,440
By the way to exit out of man type Q And it's the same way with less if I didn't mention that as well

256
00:18:53,440 --> 00:18:59,950
you just type Q to exit that page nation system there as well remembered that a shell script just executes

257
00:18:59,980 --> 00:19:05,090
ever command inside that script just as if you were typing it on the command line.

258
00:19:05,230 --> 00:19:13,360
So if we look at the contents of our script here if we were to do this pound space display low and press

259
00:19:13,360 --> 00:19:19,660
enter nothing happens because the pound sign is a comment and comments aren't executed and if we type

260
00:19:19,660 --> 00:19:23,700
in the next line in the script echo hello and press enter.

261
00:19:23,700 --> 00:19:26,100
Then we get Hello displayed to the screen.

262
00:19:26,250 --> 00:19:29,700
And that's exactly what happens when we execute the script.

263
00:19:29,700 --> 00:19:34,720
By the way this simple echo command can come in really handy when you're debugging or writing scripts.

264
00:19:34,770 --> 00:19:39,900
You can use it to show exactly what is going on inside the script along the way.

265
00:19:39,900 --> 00:19:46,560
Now let's get back to editing the script let's assign a value to a variable

266
00:19:51,770 --> 00:19:54,470
let's call this variable word.

267
00:19:54,540 --> 00:19:56,580
We'll follow that with an equal sign.

268
00:19:56,630 --> 00:20:01,750
A single quote script and end it with a single quote.

269
00:20:01,870 --> 00:20:09,290
I'm going to save my changes now variables are simply storage locations that have a name.

270
00:20:09,350 --> 00:20:15,510
In this example the name is word you can think of variables as name value pairs.

271
00:20:15,560 --> 00:20:22,070
So if we were to access the variable of word the value that would be returned would be script.

272
00:20:22,070 --> 00:20:26,810
Unlike some other programming languages when you're creating variables you don't have to specify its

273
00:20:26,810 --> 00:20:32,750
type whether it's a string or an integer or anything like that you just assign it a value.

274
00:20:32,750 --> 00:20:37,610
By the way I want to point out that it's very important that there is no space in between the equal

275
00:20:37,610 --> 00:20:38,250
sign.

276
00:20:38,330 --> 00:20:41,640
So it's the variable name followed by an equal sign without any space.

277
00:20:41,660 --> 00:20:48,440
Again without any space then the assignment in this case we're assigning a string and strings are enclosed

278
00:20:48,440 --> 00:20:52,730
in quotation marks either a single quotation marks or double quotation marks.

279
00:20:52,760 --> 00:20:57,950
By the way I recommend that you use a descriptive name for your variables although it's not required.

280
00:20:57,950 --> 00:21:00,980
This simply just makes the script easier to read.

281
00:21:00,980 --> 00:21:06,110
Now think about someone else who's going to be reading the script or maybe even yourself if it's a year

282
00:21:06,110 --> 00:21:09,680
later or a long time after you've originally written the script.

283
00:21:09,750 --> 00:21:15,050
It would be easier for you to tell what's going on if the variables had meaningful names.

284
00:21:15,050 --> 00:21:21,590
Now as a quick aside I want to tell you about someone I worked with whose last name started with Z and

285
00:21:21,590 --> 00:21:24,720
so everything he created contained a Z.

286
00:21:24,800 --> 00:21:29,420
If you looked at his home directory he had a bunch of shell scripts one shell script was name Z.

287
00:21:29,420 --> 00:21:31,660
The next shell script was named Z.

288
00:21:31,670 --> 00:21:34,750
The next shell script was named Z and there was a shell script.

289
00:21:34,790 --> 00:21:37,040
Ze one one name ze to one name.

290
00:21:37,100 --> 00:21:44,510
Xie xie xie xie 1 1 2 etc. just went crazy like he had no imagination other than the letter Z.

291
00:21:44,690 --> 00:21:48,830
And then when you opened up one of his scripts and looked in them all the variables were the same.

292
00:21:48,890 --> 00:21:52,520
Z equals blah ze 1 equals blah Z was blah.

293
00:21:52,940 --> 00:21:59,270
He was the only person that had any kind of clue of what was going on when his scripts because they

294
00:21:59,270 --> 00:22:00,800
were almost impossible to read.

295
00:22:01,430 --> 00:22:08,030
So again using something like that works it's just not recommended by the way there are some rules around

296
00:22:08,030 --> 00:22:14,270
variable names variable names can contain letters digits and underscores and they can start with letters

297
00:22:14,270 --> 00:22:18,270
or underscores but they can not start with a digit.

298
00:22:18,350 --> 00:22:21,750
So for example word is a valid variable name.

299
00:22:21,770 --> 00:22:28,470
Word 1 is a valid variable name underscore word is a valid variable name.

300
00:22:28,580 --> 00:22:33,470
However 3 word is not a valid variable name.

301
00:22:33,470 --> 00:22:41,420
Also you cannot use dashes so something like this a dash word is not a valid variable name it's invalid

302
00:22:41,420 --> 00:22:42,650
you can't use that.

303
00:22:42,830 --> 00:22:46,460
One more example would be an at sign you can't use that as well.

304
00:22:46,460 --> 00:22:51,130
E at male equals something is not going to work.

305
00:22:51,820 --> 00:22:54,820
Let me get rid of these example names here.

306
00:22:54,970 --> 00:22:59,910
Now that we've assigned a value to our variable let's go ahead and use that variable

307
00:23:07,880 --> 00:23:10,330
so we'll just to echo space.

308
00:23:10,460 --> 00:23:12,040
Double quotation marks.

309
00:23:12,110 --> 00:23:13,280
Dollar sign.

310
00:23:13,280 --> 00:23:21,690
The variable name which is a word in end it with a double quotation mark so when you reference the variable

311
00:23:21,690 --> 00:23:24,580
by name you get its value back.

312
00:23:24,720 --> 00:23:26,760
Now to do that you have to proceed.

313
00:23:26,760 --> 00:23:28,830
The variable name with a dollar sign.

314
00:23:28,830 --> 00:23:33,890
So when we want the value of word we use dollar sign word.

315
00:23:33,900 --> 00:23:40,140
Now you may have noticed that in the previous echo statement I used single quotes but in this echo statement

316
00:23:40,170 --> 00:23:45,730
I use double quotes single quotes prevent the expansion of variables.

317
00:23:45,780 --> 00:23:51,930
So if you want echo to display exactly what you specify put it in single quotes if you want variables

318
00:23:51,930 --> 00:23:54,570
to be interpreted make sure that you use double quotes.

319
00:23:54,570 --> 00:23:57,980
By the way we're just using the echo shell built in for now.

320
00:23:58,140 --> 00:24:05,220
But this concept of single quotes and double quotes apply to other commands as well to say that another

321
00:24:05,220 --> 00:24:10,140
way it's not because we're using echo that this expansion happens with double quotes and it doesn't

322
00:24:10,140 --> 00:24:14,740
with single quotes it's the quote to themselves that is causing this expansion or not.

323
00:24:14,790 --> 00:24:19,770
And then you can use this functionality with other commands not just with Echo.

324
00:24:19,770 --> 00:24:24,610
So let's execute the script and see what happens okay.

325
00:24:24,620 --> 00:24:27,140
We get Hello which is from Echo Hello.

326
00:24:27,140 --> 00:24:33,260
Then we set the word variable to equal script and then we displayed that word variable which actually

327
00:24:33,260 --> 00:24:38,030
in turn displays the word script by the way you can do what I'm doing here.

328
00:24:38,030 --> 00:24:44,540
As far as your workflow goes which is to open your editor edit the file exit your editor and then execute

329
00:24:44,540 --> 00:24:45,630
the script.

330
00:24:45,650 --> 00:24:51,020
Another thing you could do is actually open up a new terminal window connect to the vagrant system get

331
00:24:51,020 --> 00:24:56,120
into that directory and then execute that script with the other terminal so you just switch back and

332
00:24:56,120 --> 00:25:00,830
forth between terminals versus entering and exiting your editor all the time.

333
00:25:00,830 --> 00:25:03,430
Just use whatever method works best for you.

334
00:25:03,490 --> 00:25:07,790
For now for the rest of this lesson I'm just going to edit the script exit out execute the script and

335
00:25:07,790 --> 00:25:08,240
so on.

336
00:25:08,750 --> 00:25:14,410
So let's return to editing our script add a comment here of what we're about to do

337
00:25:26,140 --> 00:25:29,680
okay we're putting dollar word which is our variable inside single quotes.

338
00:25:29,680 --> 00:25:34,540
Now let's see what effect that has.

339
00:25:34,560 --> 00:25:40,200
So as you can see using Siegel quotes displays exactly what is in the single quotes it doesn't do any

340
00:25:40,200 --> 00:25:42,450
expanding or interpreting of the variables.

341
00:25:42,510 --> 00:25:44,550
It's exactly what you specify.

342
00:25:44,730 --> 00:25:47,070
So just keep that in mind if you're using variables.

343
00:25:47,070 --> 00:25:51,690
Use double quotes if you want to be very specific and make sure that nothing gets changed use single

344
00:25:51,690 --> 00:25:52,650
quotes.

345
00:25:52,660 --> 00:25:54,290
Okay let's do some more editing here

346
00:26:06,490 --> 00:26:09,460
so you can use variables with hard coded text.

347
00:26:09,460 --> 00:26:12,550
So here we have some text that we just manually typed in.

348
00:26:12,550 --> 00:26:17,710
This is a shell and then we use a variable which has dollar words so we want the value of that variable

349
00:26:17,710 --> 00:26:18,850
to be returned.

350
00:26:18,850 --> 00:26:21,640
Let's exit out and execute the script and see what happens

351
00:26:24,590 --> 00:26:46,320
so it gets interpreted and the output is this is a shell script.

352
00:26:46,490 --> 00:26:51,980
You can also enclose the variable name in curly braces and perceive the opening brace with a dollar

353
00:26:51,980 --> 00:26:52,760
sign.

354
00:26:52,760 --> 00:26:54,490
So the syntax is dollar sign.

355
00:26:54,500 --> 00:26:59,370
Opening curly brace in the variable name followed by a closing curly brace.

356
00:26:59,450 --> 00:27:04,490
By the way when you're reading scripts written by others you'll see both types of syntax being used.

357
00:27:04,520 --> 00:27:09,890
You'll see the dollar sign with a variable name as well as the dollar sign including the braces in the

358
00:27:09,890 --> 00:27:11,000
variable name.

359
00:27:11,000 --> 00:27:19,700
Now let's execute our script and see what happens so we get the same exact output as the preceding echo

360
00:27:19,700 --> 00:27:20,330
command.

361
00:27:20,360 --> 00:27:30,020
It's we're just using the different syntax.

362
00:27:30,070 --> 00:27:34,180
Now if we want to append text to the variable we have to use this dollar sign.

363
00:27:34,180 --> 00:27:35,320
Brace syntax.

364
00:27:35,530 --> 00:27:36,690
Let me show an example.

365
00:27:43,460 --> 00:27:50,860
So if we want to append AMG directly after the output of the variable we have to use the curly braces.

366
00:27:50,930 --> 00:27:57,920
Otherwise bash doesn't know that the variable is actually word W O R D and that the part that's not

367
00:27:57,920 --> 00:27:59,390
the variable is I N G.

368
00:27:59,390 --> 00:28:05,960
It has no way of knowing that unless you use the braces to separate the variable from the preceding

369
00:28:05,990 --> 00:28:07,430
or following text.

370
00:28:07,430 --> 00:28:11,090
Now let's execute this.

371
00:28:11,300 --> 00:28:13,790
Says scripting is fun again.

372
00:28:13,790 --> 00:28:20,680
Script is what was stored and the word variable i n g is what we card coded in the echo statement there.

373
00:28:24,420 --> 00:28:28,530
So let's demonstrate how not to append text to a variable I was just talking about this.

374
00:28:28,530 --> 00:28:36,490
Let's do this.

375
00:28:36,530 --> 00:28:43,480
So this is not gonna work echo dollar sign a word IAG is a fine again.

376
00:28:43,530 --> 00:28:49,620
BASH doesn't know that high energy is not part of the variable name by the way I didn't necessarily

377
00:28:49,620 --> 00:28:55,910
mention this but by convention not by rule or syntax variables are in all uppercase.

378
00:28:55,920 --> 00:28:59,220
That's why you used uppercase word as a variable name.

379
00:28:59,280 --> 00:29:03,960
And that way you can typically easily spot what is a variable and what is not.

380
00:29:03,960 --> 00:29:11,570
However since that's only a convention or a best practice and it's not enforceable by syntax then bash

381
00:29:11,580 --> 00:29:15,660
isn't going oh I N G is lowercase so therefore it's not part of the variable name.

382
00:29:15,660 --> 00:29:21,210
No that's not how it works you can actually use lower case variables and some people do so this will

383
00:29:21,270 --> 00:29:22,070
not work.

384
00:29:22,080 --> 00:29:22,910
Let's prove it here.

385
00:29:25,570 --> 00:29:29,170
You get blank is fun for the last bit of output.

386
00:29:29,260 --> 00:29:35,850
That's because dollar word IAG that variable doesn't exist so it doesn't expand to anything so there's

387
00:29:35,860 --> 00:29:36,870
nothing there.

388
00:29:37,060 --> 00:29:38,560
And it displays nothing.

389
00:29:38,800 --> 00:29:43,180
And that was followed by a space is space fun exclamation point.

390
00:29:43,210 --> 00:29:45,760
So that is what it gets displayed on the screen.

391
00:29:45,760 --> 00:29:47,340
Space is space fun.

392
00:29:47,350 --> 00:29:48,210
Exclamation point.

393
00:29:50,910 --> 00:29:57,540
Now let's create another variable for a script here.

394
00:29:57,580 --> 00:30:00,760
We're going to name this variable ending.

395
00:30:00,760 --> 00:30:06,070
Again we're going to use all uppercase letters because that is the convention or best practice not required

396
00:30:06,100 --> 00:30:07,870
but that's how we're going to do it.

397
00:30:07,930 --> 00:30:11,040
We're not going to use spaces because that will cause an error.

398
00:30:11,050 --> 00:30:13,480
So we want to make sure there are no spaces here.

399
00:30:13,480 --> 00:30:19,300
We're going to use single quotes and type in E D and close it was single quotes because we're not doing

400
00:30:19,300 --> 00:30:21,250
any variable expansion here.

401
00:30:21,250 --> 00:30:26,210
We're just doing a strict assignment of E D to this variable.

402
00:30:26,230 --> 00:30:29,020
Now let's combine these two variables that we've created

403
00:30:41,470 --> 00:30:45,130
so as you can see we use this dollar sign brace syntax.

404
00:30:45,130 --> 00:30:51,550
So this variable of word will get interpreted and then dollar sign ending here will get interpreted

405
00:30:51,550 --> 00:30:52,490
as well.

406
00:30:52,540 --> 00:30:59,230
So we'll get this is script and this should expand to add E.D. here.

407
00:30:59,230 --> 00:31:01,810
So let's prove this by executing it on the command line.

408
00:31:04,150 --> 00:31:09,790
Sure enough we get this is scripted because the word variable contains the value of script and the ending

409
00:31:10,090 --> 00:31:16,040
variable contains the value of E D.

410
00:31:16,040 --> 00:31:26,000
Now let's change the value that stored in the ending variable.

411
00:31:26,030 --> 00:31:34,450
This is called reassignment by the way.

412
00:31:34,660 --> 00:31:39,370
Now we're going to change the value that stored an ending to AI in G.

413
00:31:39,430 --> 00:31:45,690
By the way you can overwrite the contents of the variables just using assignments or these reassignments.

414
00:31:45,700 --> 00:31:51,400
So the first time ending is specified it's equal to E.D. when you access the ending variable.

415
00:31:51,400 --> 00:31:52,960
That's what gets displayed.

416
00:31:53,080 --> 00:31:57,580
But since the scripts are read from the top down just as if you were typing them in at the command line

417
00:31:57,880 --> 00:32:03,360
when you changed the value of ending then anything that follows that will use that new value.

418
00:32:03,460 --> 00:32:13,370
And let's prove that here we'll do echo dollar sign a word dollar sign ending is fun see where our changes

419
00:32:13,370 --> 00:32:17,450
and execute our script so we have scripting is fun.

420
00:32:17,460 --> 00:32:24,510
So word expanded to script the ending since we changed it to i n g span into AMG so scripting is what

421
00:32:24,510 --> 00:32:27,300
you get when you combine word and ending together.

422
00:32:27,390 --> 00:32:33,510
Again we use that special brace syntax so that that's cartel where one variable started and ended as

423
00:32:33,510 --> 00:32:36,410
well as the other variable where it started and ended.

424
00:32:39,430 --> 00:32:41,080
Let's do one more reassignment

425
00:32:46,650 --> 00:32:48,390
will assign s to ending

426
00:33:01,350 --> 00:33:06,290
hey let's save our changes and execute our script.

427
00:33:06,380 --> 00:33:10,370
You were going to write many scripts in this class all right.

428
00:33:10,370 --> 00:33:15,950
This brings us to the end of this lesson in this lesson you've learned a lot you've learned about file

429
00:33:15,950 --> 00:33:22,370
naming file naming conventions how file names don't determine if something is a script or executable

430
00:33:22,370 --> 00:33:22,810
or not.

431
00:33:22,820 --> 00:33:29,150
It's actually the execute bit on the file itself that determines if you can execute it as a script or

432
00:33:29,150 --> 00:33:30,050
not.

433
00:33:30,050 --> 00:33:35,750
You also learned about beings you learned how to use comments effectively create a header in your file

434
00:33:35,750 --> 00:33:36,250
and comment.

435
00:33:36,260 --> 00:33:41,750
The script along the way you learned about the difference between a shell built in versus a normal command

436
00:33:41,750 --> 00:33:42,560
on the system.

437
00:33:42,680 --> 00:33:48,620
How to get help with shell built ends which is to specify the word help followed by the name of the

438
00:33:48,830 --> 00:33:50,180
shell built in.

439
00:33:50,180 --> 00:33:55,130
You also learn that you can get help about general commands on the system using man pages.

440
00:33:55,130 --> 00:34:01,550
So man followed by the command again man and a shell built in will not provide any help but you can

441
00:34:01,550 --> 00:34:05,800
get help from man bash or help and the shell built in.

442
00:34:05,810 --> 00:34:09,860
Finally we talked about variables how to assign them not to reassign them.

443
00:34:09,860 --> 00:34:15,140
We talked about single quotes and how nothing inside single quotes gets expanded or interpreted and

444
00:34:15,140 --> 00:34:21,590
how things since I double quotes to get expanded interpreted such as variables we talked about the two

445
00:34:21,620 --> 00:34:26,570
different syntax as you can use with variables one is just pre spending a dollar assigned to the variable

446
00:34:26,570 --> 00:34:32,540
name and the other is pre printing a dollar sign and surrounding the variable in braces you learned

447
00:34:32,540 --> 00:34:40,700
that that second syntax is required if you want to immediately precede or immediately follow that variable

448
00:34:40,730 --> 00:34:47,690
with some additional data even though using the dollar sign and brace encapsulation method is a best

449
00:34:47,690 --> 00:34:50,240
practice when working with variables.

450
00:34:50,240 --> 00:34:55,220
I showed you the other method so that if you see other people's scripts you know what they're doing

451
00:34:55,220 --> 00:34:57,760
and it doesn't come as a surprise to you.

452
00:34:57,940 --> 00:34:59,710
Okay that wraps it up for this lesson.
