1
00:00:03,670 --> 00:00:09,090
In this lesson you're going to learn about some special variables created by the shell at a store the

2
00:00:09,090 --> 00:00:11,000
output of a command in a variable.

3
00:00:11,100 --> 00:00:16,080
And we're going to talk a little bit about the if statement first thing I'm going to do here is open

4
00:00:16,080 --> 00:00:18,090
up a terminal on my local machine.

5
00:00:18,270 --> 00:00:23,370
I'm going to go into our class folder of show class room here.

6
00:00:23,370 --> 00:00:27,210
I'm going to go into a local user CD local users.

7
00:00:27,210 --> 00:00:33,100
We're still working on creating local users and I'm going to start this virtual machine

8
00:00:41,080 --> 00:00:47,780
once the machine is booted you can connect to it with vagrant S-sh.

9
00:00:47,780 --> 00:00:53,940
Now let's move into our shared folder of Ford slash vagrant Tim get there with a cd command.

10
00:00:54,380 --> 00:01:02,510
Now let's start working on the script for this lesson Minick called this L user demo to dot S H.

11
00:01:02,510 --> 00:01:09,470
Remember we always start our scripts so the banks are going to start out with a sharp pain or spin or

12
00:01:09,530 --> 00:01:11,250
slash Bash.

13
00:01:11,360 --> 00:01:16,830
As you probably remember from the last lesson I like to put a header at the top of the file that just

14
00:01:16,850 --> 00:01:18,950
describes what the shell script is going to do.

15
00:01:18,950 --> 00:01:20,700
I'm going to put that in right now.

16
00:01:35,890 --> 00:01:41,350
So these are the two main goals that I have for this script which is to display the idea which stands

17
00:01:41,350 --> 00:01:48,730
for the user id or user identification number and the user name of the user that's executing this particular

18
00:01:48,730 --> 00:01:49,520
script.

19
00:01:49,630 --> 00:01:54,760
And the last thing we want to do in this script is to display if the user that is executing the script

20
00:01:55,030 --> 00:02:00,910
is the root user or if they are some other user they're either the root user executing the script or

21
00:02:00,910 --> 00:02:02,000
they're not.

22
00:02:02,200 --> 00:02:07,090
One of the things that can help you write shell scripts is to think about all the steps that it takes

23
00:02:07,090 --> 00:02:08,590
to accomplish your goal.

24
00:02:08,590 --> 00:02:10,180
We have the goal in the header.

25
00:02:10,300 --> 00:02:16,230
So now let's think about some steps that we need to take in order to accomplish our goal.

26
00:02:16,240 --> 00:02:18,310
A lot of people call this pseudo coding.

27
00:02:18,310 --> 00:02:24,190
And so what you do is you just write in plain English and your language of what you want to accomplish

28
00:02:24,220 --> 00:02:25,380
as a comment.

29
00:02:25,490 --> 00:02:31,750
But those comments in your file and then go back and put in the exact shell commands and syntax that

30
00:02:31,750 --> 00:02:33,060
you'll need to accomplish.

31
00:02:33,190 --> 00:02:38,250
Each one of those tasks so it's kind of like outlining before you write the shell script.

32
00:02:38,260 --> 00:02:44,280
So I'm going to go ahead and do that now the first thing I want to do is display the ID.

33
00:02:44,440 --> 00:02:49,260
The next thing I want to do is display a user name.

34
00:02:49,320 --> 00:02:56,790
And finally what I want to do is display if the user is the root user or not.

35
00:02:58,180 --> 00:03:03,850
So now we have our Hetter and our pseudo code now and start filling in the code.

36
00:03:03,850 --> 00:03:06,990
So the first thing we want to do is display the ID.

37
00:03:07,120 --> 00:03:12,280
And of course you know that the echo shell built in displays information to the screen and we're going

38
00:03:12,280 --> 00:03:21,100
to use something that the shell gives us for free I might show you here your ID is you ID as you know

39
00:03:21,100 --> 00:03:26,590
if you perceive something with a dollar sign and enclose it in curly braces that it's a variable.

40
00:03:26,590 --> 00:03:27,980
This variable is you.

41
00:03:28,000 --> 00:03:30,690
But we didn't assign a variable or set a variable.

42
00:03:30,790 --> 00:03:33,560
So where is that variable going to come from.

43
00:03:33,730 --> 00:03:37,440
Well remember that I said you get a lot of stuff quote stuff for free.

44
00:03:37,540 --> 00:03:42,940
When you use bash one of the things you get for free are special preset variables.

45
00:03:42,970 --> 00:03:47,170
So let's go ahead and write and quit our script.

46
00:03:47,170 --> 00:03:51,240
Remember before you execute the script for the first time you want to set the executable bit on it.

47
00:03:54,430 --> 00:03:56,990
And now let's execute our script and see what happens.

48
00:03:58,700 --> 00:04:01,630
Hey it says your ID is 1000.

49
00:04:01,830 --> 00:04:06,450
So let's look at the bash man page to see where this UAD comes from.

50
00:04:08,240 --> 00:04:13,070
By the way to navigate a man page you're going to use vi or VM keybindings.

51
00:04:13,070 --> 00:04:17,640
And so how to search for it is a forward slash and then the string you're searching for.

52
00:04:17,660 --> 00:04:19,420
We're looking for you.

53
00:04:19,450 --> 00:04:24,390
So we'll do that and hit enter to take us to the first instance of that.

54
00:04:24,450 --> 00:04:29,610
And if we want to keep going down the file it in to go to the next occurrence.

55
00:04:29,960 --> 00:04:35,630
And here you can see that the information about this says that it expands to the ID of the current user

56
00:04:35,750 --> 00:04:37,700
initialized at Shell startup.

57
00:04:37,700 --> 00:04:39,890
This variable is a read only.

58
00:04:39,890 --> 00:04:42,410
That means that a user can't change it.

59
00:04:42,410 --> 00:04:48,380
So if we were to check against this ID we can be fairly certain that it's going to be the actual source

60
00:04:48,380 --> 00:04:48,890
of truth.

61
00:04:48,890 --> 00:04:52,000
It's really going to have the you ID in it.

62
00:04:53,440 --> 00:04:55,100
Now let me back up a little bit.

63
00:04:55,100 --> 00:04:58,730
I'm going to hit shift and to back up to the previous match.

64
00:04:58,760 --> 00:05:05,300
Now there is a similar variable here called the you I.D. which expands to the effective user id of the

65
00:05:05,300 --> 00:05:08,030
current user initialized at Shell startup.

66
00:05:08,030 --> 00:05:10,960
This very well is also read only.

67
00:05:11,090 --> 00:05:15,230
So in reality we could use EU ID or ID.

68
00:05:15,410 --> 00:05:23,660
The only time that you ID would be different from EU ID is in the case of a set id script when a file

69
00:05:23,660 --> 00:05:26,210
has a special set id permissions set on it.

70
00:05:26,300 --> 00:05:32,330
The file actually gets executed as the owner of the file no matter what user is executing the file.

71
00:05:32,420 --> 00:05:39,880
So if its set you ID root and where the vagrant user and we execute the script are you ID is 1004 vagrant.

72
00:05:39,890 --> 00:05:43,070
But r e ID is 0 for root.

73
00:05:43,100 --> 00:05:49,880
However this is rarely seen modern Linux systems for security reasons do not allow scripts to be set.

74
00:05:49,880 --> 00:05:50,910
You ID.

75
00:05:51,020 --> 00:05:56,510
So this is just something to keep in mind for example if you're working on say a proprietary UNIX system

76
00:05:56,510 --> 00:05:57,660
that's somewhat old.

77
00:05:57,830 --> 00:06:03,950
But again if you're working on modern day Linux systems you can safely use you ID because e you ID and

78
00:06:03,950 --> 00:06:08,510
you ID are going to be the same but just know if you see that somewhere else that is where that comes

79
00:06:08,510 --> 00:06:09,050
from.

80
00:06:09,290 --> 00:06:17,310
OK let's back up even farther Monotype questionmark for a reverse search and type shell and hit enter

81
00:06:17,310 --> 00:06:18,210
here.

82
00:06:18,210 --> 00:06:24,420
So the ID and you ID that we were just reading about happened to be in the shell variables section of

83
00:06:24,420 --> 00:06:26,140
the bash man page.

84
00:06:26,370 --> 00:06:32,880
So not only do you get e ID and you ID you get several other variables that anytime you start Bash that

85
00:06:32,940 --> 00:06:34,760
all these are going to be set.

86
00:06:35,130 --> 00:06:40,010
So if you want take a moment here and just read down these variables and see what's available to you.

87
00:06:41,580 --> 00:06:48,390
OK I'm just going to hit Q to quit since we're done looking at the man page and I quickly want to demonstrate

88
00:06:48,390 --> 00:06:51,340
that to you ID is indeed read only.

89
00:06:51,420 --> 00:06:56,420
So if we do echo your I.D. It's 1000.

90
00:06:56,720 --> 00:07:03,100
If we do you ID equals let's say we're going to set it to 1000 of one hit enter.

91
00:07:03,170 --> 00:07:06,880
It says no you can't do that because it's a read only variable.

92
00:07:07,070 --> 00:07:10,820
So again we can trust the information that's in the ID variable.

93
00:07:10,880 --> 00:07:15,110
So if you remember from our script the next thing we want to do is actually display the user name.

94
00:07:15,110 --> 00:07:16,520
So how do we do that.

95
00:07:16,580 --> 00:07:22,440
Well one way to do that is with the ID command and then see this type dash a ID.

96
00:07:22,520 --> 00:07:26,360
So it is actually a program and it's not a shell built in.

97
00:07:26,390 --> 00:07:32,720
So since it's not a shell built in we can learn about it by using the man page man id by the way when

98
00:07:32,720 --> 00:07:34,610
you're looking at a man page.

99
00:07:34,640 --> 00:07:39,820
If you see things that are enclosed in square brackets that means they are optional.

100
00:07:39,980 --> 00:07:44,330
So here you can see ID with optional option an optional user.

101
00:07:44,450 --> 00:07:48,620
So actually you can run this command on its own and doesn't require any arguments because they're all

102
00:07:48,620 --> 00:07:49,430
optional.

103
00:07:49,670 --> 00:07:55,340
Additionally if you see these three dots or three periods also called an ellipsis I do believe that

104
00:07:55,340 --> 00:07:58,430
you can specify multiple of whatever precedes it.

105
00:07:58,430 --> 00:08:05,540
So you can have the command followed by multiple options and then one optional user.

106
00:08:05,690 --> 00:08:11,180
So the description here says print user and group information for the specified user or when user omitted

107
00:08:11,240 --> 00:08:13,540
for the current user.

108
00:08:13,570 --> 00:08:18,330
So I'm going to look at what our options are here dash and we'll print a name instead of a number.

109
00:08:18,340 --> 00:08:23,650
And so we are actually looking to print a name so that's useful that you print only the effective user

110
00:08:23,650 --> 00:08:24,460
ID.

111
00:08:24,490 --> 00:08:32,130
So those look like the two things that we need to use to print a user name would be Dasch you in the

112
00:08:32,490 --> 00:08:39,040
queue here and just test out the ID command with no options ID it enter and says Are you ID is 1000

113
00:08:39,130 --> 00:08:39,610
vagrant.

114
00:08:39,610 --> 00:08:41,030
Is our user name.

115
00:08:41,070 --> 00:08:46,110
The idea is 1000 which is a group ID and vagrant is the username of that group and the groups that were

116
00:08:46,120 --> 00:08:50,030
member of is also called vagrant with a group id of 1000.

117
00:08:50,380 --> 00:08:56,520
So now let's specify ideaspace dash you and that prints out the ID of 1000.

118
00:08:56,770 --> 00:09:02,770
Now if we wanted to add the dash in option we could do this ID dash you dash and and it displays our

119
00:09:02,820 --> 00:09:04,240
user name.

120
00:09:04,340 --> 00:09:08,140
Also want to point out that it doesn't matter about the order of these options.

121
00:09:08,260 --> 00:09:11,850
So I can do id dash and DSU and get the same result.

122
00:09:12,130 --> 00:09:18,660
Another thing I want to point out is when you have these single character options you can combine them.

123
00:09:18,760 --> 00:09:21,000
So will do id dash.

124
00:09:21,220 --> 00:09:25,830
And then we can list all the single character options that we need without any for the dashes.

125
00:09:25,840 --> 00:09:31,720
So we'll do dash and you and that will give us our user name or it doesn't matter about the order so

126
00:09:31,720 --> 00:09:37,510
we can do id space dash you and press enter and we get the same result.

127
00:09:37,720 --> 00:09:43,780
For me Id dash you in is a steadily pleasing because it's you are a user and for user name so that makes

128
00:09:43,780 --> 00:09:45,910
sense to me that's the command I'm going to use.

129
00:09:45,910 --> 00:09:51,320
But any of those variations work obviously so if you see them you'll know what's going on.

130
00:09:51,430 --> 00:09:55,730
By the way some of you may be familiar with the Who am I command will execute it now.

131
00:09:56,110 --> 00:09:57,670
And that would be another option.

132
00:09:57,670 --> 00:09:59,920
Let's look at what type of command it is.

133
00:10:01,560 --> 00:10:04,990
It is a program so I'm going to use the man page to learn more about it.

134
00:10:05,840 --> 00:10:06,460
Who am I.

135
00:10:06,460 --> 00:10:08,250
Print effective user ID.

136
00:10:08,250 --> 00:10:08,760
Who am I.

137
00:10:08,760 --> 00:10:11,180
Again you can specify options.

138
00:10:11,190 --> 00:10:15,080
The options are optional and you can specify more than one of them.

139
00:10:15,090 --> 00:10:21,180
The description is prent the user name associated with the current effective user id same as ID dash

140
00:10:21,270 --> 00:10:22,320
you in.

141
00:10:22,320 --> 00:10:24,990
So that's what we're going to use ID dashi when you can use.

142
00:10:24,990 --> 00:10:25,610
Who am I.

143
00:10:25,610 --> 00:10:29,690
If you would like as well hey let's return to editing the script.

144
00:10:32,850 --> 00:10:38,820
Before we display the user name let's assign the user name to a variable we'll call that variable user

145
00:10:38,820 --> 00:10:44,850
underscore name.

146
00:10:44,960 --> 00:10:50,450
If you want to store the output of a command into a variable you use the syntax you see here on your

147
00:10:50,450 --> 00:10:55,920
screen which is a variable name equals dollar sign opening parentheses.

148
00:10:56,000 --> 00:11:01,730
Then the command and the clothing parentheses the value for the variable name user underscore name will

149
00:11:01,730 --> 00:11:04,860
equal the output of ID dash you in.

150
00:11:05,030 --> 00:11:07,470
Let's test this here let's echo it to our screen.

151
00:11:14,670 --> 00:11:16,920
Him to say my changes and execute the script

152
00:11:20,660 --> 00:11:22,860
so it says your ID is 1000.

153
00:11:22,880 --> 00:11:30,840
Your user name is vagrant want to demonstrate an alternative syntax and you'll see sometimes as well.

154
00:11:30,960 --> 00:11:35,060
So let's edit our script let's comment out this line.

155
00:11:35,240 --> 00:11:36,890
I'm going to make a copy of it.

156
00:11:37,110 --> 00:11:44,670
And then instead of dollar sign opening parentheses I'm going to replace that with a tick and the closing

157
00:11:44,670 --> 00:11:50,010
parentheses I'm going to replace that with a tick as well going to execute the script and see if we

158
00:11:50,010 --> 00:11:51,230
get the same result.

159
00:11:51,540 --> 00:11:54,650
And sure enough we do let's get back into here.

160
00:11:54,840 --> 00:11:57,310
So that is an older style syntax.

161
00:11:57,330 --> 00:12:03,730
The tick marks that surround a command to assign that to a variable that's the Oler syntax.

162
00:12:03,900 --> 00:12:08,700
So again I like to show you these things because different people write shell scripts in different ways

163
00:12:08,730 --> 00:12:14,370
and you'll be looking at Shell scripts from perhaps earlier times that were written without these modern

164
00:12:14,370 --> 00:12:20,010
conventions so if you see these back Tick's know that it's the same thing as the dollar sign parentheses

165
00:12:20,010 --> 00:12:21,280
syntax.

166
00:12:21,280 --> 00:12:26,990
So going to remove that line uncomment that other line and we should be back to where we were.

167
00:12:27,360 --> 00:12:30,720
For someone to type out an if statement here and then we'll talk through it.

168
00:12:50,280 --> 00:12:57,990
So an if statement begins with the key word if followed by a space then double opening brackets followed

169
00:12:57,990 --> 00:13:05,670
by a space and then an expression after that expression you'll use a space and then the closing double

170
00:13:05,670 --> 00:13:11,880
brackets on the next line you'll use the keyword then and then whatever follows that will get executed

171
00:13:11,910 --> 00:13:14,560
if the expression evaluates to true.

172
00:13:14,820 --> 00:13:22,320
So in our case if the ID is equal to zero then we'll execute the code that says echo you are root.

173
00:13:22,680 --> 00:13:28,350
If you want to include an else statement then you use the keyword and then the code that follows that

174
00:13:28,350 --> 00:13:29,120
will execute.

175
00:13:29,130 --> 00:13:36,360
If the expression evaluates to false so if ID is not equal to zero then what's going to happen is Echo

176
00:13:36,360 --> 00:13:42,810
will be run with you are not root displaying to the screen you close your if statement with F I which

177
00:13:42,810 --> 00:13:44,400
is the reverse of I-F.

178
00:13:44,610 --> 00:13:48,140
And that is the general syntax for the if statement.

179
00:13:48,240 --> 00:13:50,400
I want to point out a couple of different things here again.

180
00:13:50,400 --> 00:13:53,310
The F word should be followed by a space.

181
00:13:53,430 --> 00:13:58,530
The opening brackets should be followed by a space and the closing double bracket should have a space

182
00:13:58,530 --> 00:13:59,770
preceding it.

183
00:13:59,940 --> 00:14:05,910
When you are using variables inside of these double brackets as a test you always want to enclose them

184
00:14:05,910 --> 00:14:07,310
in quotation marks.

185
00:14:07,590 --> 00:14:11,850
OK let's get back to the shell and learn a little bit more about this statement.

186
00:14:12,360 --> 00:14:13,970
So let's do type dash a.

187
00:14:13,980 --> 00:14:18,950
If it turns out that it is a shell keyword so we can get help on that with help.

188
00:14:19,020 --> 00:14:20,190
I have.

189
00:14:20,360 --> 00:14:26,420
So here it says if commands semi-colon then commands a semi-colon and then as you already know things

190
00:14:26,420 --> 00:14:28,410
that are in brackets are optional.

191
00:14:28,510 --> 00:14:31,340
You can absolutely use Elif or else.

192
00:14:31,460 --> 00:14:34,770
And then what is not optional is the closing f I.

193
00:14:34,790 --> 00:14:42,260
So if you remember I said we were going to do this we're going to use F and then double brackets here

194
00:14:42,320 --> 00:14:57,940
and do a test.

195
00:14:57,970 --> 00:15:04,270
So what I want to point out is in the if description it says if commands a semi-colon then commands

196
00:15:04,270 --> 00:15:06,900
a semi-colon what the semi-colon is.

197
00:15:06,900 --> 00:15:12,520
Is the command separator another command separator is to hit enter or begin on a new line.

198
00:15:12,700 --> 00:15:18,310
So if we bring this last command in our shell history back up I'll just hit the up arrow you can see

199
00:15:18,310 --> 00:15:22,020
that where there were line breaks when I entered them in on the command line.

200
00:15:22,150 --> 00:15:27,790
They are now replaced with semi-colon so again the semi-colon is a command separator so any time you

201
00:15:27,790 --> 00:15:30,370
see a semi-colon in some of these descriptions.

202
00:15:30,400 --> 00:15:36,010
Know that you can use a new line and I like to use the new line so it doesn't run altogether and it

203
00:15:36,010 --> 00:15:39,350
looks visually better to me and it's easier for me to read.

204
00:15:39,640 --> 00:15:45,380
However it is correct to also use a semi-colon and keep it all on one line.

205
00:15:45,760 --> 00:15:50,560
I just want to point out that if your command is not completed when you hit enter you'll get a different

206
00:15:50,560 --> 00:15:53,130
looking prompt here which is the greater than sign.

207
00:15:53,320 --> 00:15:57,310
And then once the command is done then you'll get your normal prompt back.

208
00:15:57,310 --> 00:15:58,950
I just wanted to point that out.

209
00:15:59,350 --> 00:16:01,060
So let's keep reading about the above statement.

210
00:16:01,060 --> 00:16:04,870
The IF command list is executed you can think a list is a list of commands.

211
00:16:04,920 --> 00:16:13,370
If the IF commands list is executed if its exit status is zero then the then commands list is executed.

212
00:16:13,420 --> 00:16:17,360
You can think of an exit status of 0 being true.

213
00:16:17,360 --> 00:16:20,850
So if this is true then DO THAT else.

214
00:16:20,920 --> 00:16:23,200
If it's not true then do something else.

215
00:16:23,350 --> 00:16:26,480
So you have a general idea of how the statement works.

216
00:16:26,680 --> 00:16:33,370
However in the if description it says if commands and if those commands have an exit status zero then

217
00:16:33,370 --> 00:16:34,650
execute some code.

218
00:16:34,870 --> 00:16:37,250
Well where do we get these double brackets.

219
00:16:37,300 --> 00:16:44,310
Well let me show you those double brackets are actually a shell keyword so let's get help on those.

220
00:16:48,620 --> 00:16:54,590
So here it says double brackets expression closing double brackets also says it returns a status of

221
00:16:54,590 --> 00:16:59,260
0 or 1 depending on the evaluation of the conditional expression expression.

222
00:16:59,450 --> 00:17:04,040
Expressions are composed of the same primary is used by the test built in and may be combined using

223
00:17:04,040 --> 00:17:05,570
the following operators.

224
00:17:05,570 --> 00:17:07,360
We're not going to worry about those operators for a moment.

225
00:17:07,370 --> 00:17:12,320
But the thing to keep in mind here is that it says you can use the same things that you can use with

226
00:17:12,320 --> 00:17:15,330
a test built in to generate your expressions.

227
00:17:15,520 --> 00:17:16,050
So

228
00:17:18,670 --> 00:17:22,260
test is indeed a shell built in as well as a command.

229
00:17:22,420 --> 00:17:25,240
We're interested in using the built in so we'll get help with it.

230
00:17:25,240 --> 00:17:31,030
Will do help test and I know from experience that a lot of information is going to scroll off the screen

231
00:17:31,030 --> 00:17:37,050
so one way to prevent that is to use a pager so will use a pipe to pipe the output of help test into

232
00:17:37,060 --> 00:17:40,110
our pager of Lessel Let's hit and and see what happens.

233
00:17:41,020 --> 00:17:44,650
So the test a built in evaluates conditional expressions.

234
00:17:44,650 --> 00:17:48,930
Here again to just reiterate what you already know 0 is true one is false.

235
00:17:49,180 --> 00:17:51,030
So here are all the tests that you can do.

236
00:17:51,030 --> 00:17:57,370
You can do file operators like Dasch a file to test if a file exists Dasch be if its a block special

237
00:17:57,370 --> 00:17:59,760
device and so on Dashti if its a directory.

238
00:17:59,950 --> 00:18:03,250
So there are all these types of tests you can use against files.

239
00:18:03,520 --> 00:18:09,700
Additionally there are string operators so dash Z string returns true if the string is empty or Dasch

240
00:18:09,700 --> 00:18:11,030
in string returns true.

241
00:18:11,050 --> 00:18:12,440
The string is not empty.

242
00:18:12,460 --> 00:18:18,690
You can compare strings with the equal sign or the not equal that less than and greater than etc..

243
00:18:19,670 --> 00:18:25,120
This is actually what we're doing in our shellscript We're comparing the number of the ID to zero word

244
00:18:25,220 --> 00:18:29,490
saying is the ID dash Q Which stands for equal to zero.

245
00:18:29,960 --> 00:18:35,520
If it is equal then that's true and it returns a zero Q to exit out of the pager.

246
00:18:35,960 --> 00:18:38,950
OK let's execute our script and see what result we get.

247
00:18:42,880 --> 00:18:44,820
Says here you ideas 1000.

248
00:18:44,830 --> 00:18:46,300
Your user name is vagrant.

249
00:18:46,330 --> 00:18:48,490
You are not root.

250
00:18:48,490 --> 00:18:53,980
By the way if I felt the mention of the idea of zero is always assigned to the root user root always

251
00:18:53,980 --> 00:18:58,000
has the ID of 0 on every Linux system that you'll ever encounter.

252
00:18:58,000 --> 00:19:02,590
Every Unix system that you'll ever encounter route is always the idea of zero.

253
00:19:02,590 --> 00:19:05,310
That's why we checked against it because it's a guarantee.

254
00:19:05,350 --> 00:19:10,390
You could in theory test against the username but it's probably a little bit more safe and secure to

255
00:19:10,390 --> 00:19:12,320
test against ID zero.

256
00:19:12,400 --> 00:19:17,050
Because again that is always 100 percent always assigned to the root user.

257
00:19:17,260 --> 00:19:21,040
Speaking a root let's execute this program as the root user.

258
00:19:21,040 --> 00:19:25,270
One way to do that is use the command which stands for super user.

259
00:19:25,300 --> 00:19:27,070
Do so we'll do Sudoku.

260
00:19:27,260 --> 00:19:30,940
No user demo or to SH and hit enter.

261
00:19:31,360 --> 00:19:35,840
So now since we're using the sudo command this command actually gets executed as root.

262
00:19:35,950 --> 00:19:38,280
So it says your ID is 0.

263
00:19:38,290 --> 00:19:41,860
Your user name is root you are root.

264
00:19:42,100 --> 00:19:44,940
We could have also switched to the root user and done this as well.

265
00:19:44,960 --> 00:19:45,810
Can do su.

266
00:19:45,850 --> 00:19:52,730
And by the way the password for the root user on this system is vagrant and then now we can execute

267
00:19:52,730 --> 00:19:57,940
that as the root user and it says here you ID zero your username as root.

268
00:19:57,940 --> 00:19:59,280
You are definitely root.

269
00:19:59,320 --> 00:20:03,190
OK I'm going to exit back out to the vagrant user.

270
00:20:03,190 --> 00:20:04,810
Let's look at the contents of our file.

271
00:20:04,810 --> 00:20:05,890
One more time.

272
00:20:09,830 --> 00:20:17,510
I want to point out that this double bracket syntax we're using with the if statement is a bash specific

273
00:20:17,510 --> 00:20:18,410
thing.

274
00:20:18,410 --> 00:20:21,310
So this may not be portable to all shells.

275
00:20:21,320 --> 00:20:25,030
For example if you use NCSA script that's probably not going to work for you.

276
00:20:25,040 --> 00:20:27,320
But if you're using bash it will work.

277
00:20:27,320 --> 00:20:32,950
Another thing to note about the double bracket syntax is that it really replace the single bracket syntax.

278
00:20:33,050 --> 00:20:37,080
So actually you may see some shell scripts that look like this.

279
00:20:37,150 --> 00:20:42,080
If with a single opening bracket then a conditional expression and a single closing bracket.

280
00:20:42,380 --> 00:20:45,710
So again just know that the double brackets is the new way to do it.

281
00:20:45,710 --> 00:20:49,180
You can also use the single brackets that's an older way to do it.

282
00:20:49,190 --> 00:20:52,690
Let's get out to the shell here and we can actually do this.

283
00:20:53,150 --> 00:21:00,210
The single opening bracket is a shell built in but it's actually also a command on the file system.

284
00:21:00,380 --> 00:21:03,990
So on older Unix systems this is how it would work.

285
00:21:06,250 --> 00:21:09,110
So sure enough that's a file that's located at user bin.

286
00:21:09,120 --> 00:21:13,130
Opening bracket it's a file and you can execute it like that.

287
00:21:13,170 --> 00:21:18,910
So if you're using a really slimmed down shell that doesn't have the opening bracket built in the shell

288
00:21:18,910 --> 00:21:22,410
script will default to using user bin and the opening bracket.

289
00:21:22,410 --> 00:21:24,860
However we're using bash We don't have to worry about that.

290
00:21:24,960 --> 00:21:31,470
And again we're going to use the newer syntax of the double brackets OK let's look at our script one

291
00:21:31,560 --> 00:21:32,760
last time.

292
00:21:32,760 --> 00:21:34,570
Now this last a block of code.

293
00:21:34,570 --> 00:21:35,510
This if statement.

294
00:21:35,520 --> 00:21:38,010
If the UI equals zero then you are root.

295
00:21:38,010 --> 00:21:39,540
Else you are not root.

296
00:21:39,540 --> 00:21:45,810
Some people call this type of checking idiot checking and it's to help people from doing idiotic things

297
00:21:46,560 --> 00:21:49,120
other people call it sanity checking.

298
00:21:49,530 --> 00:21:55,140
And by the way it's a good idea to check for any requirements in your shellscript if you know something

299
00:21:55,140 --> 00:21:58,500
that is required for the script to work properly check for it.

300
00:21:58,500 --> 00:22:02,550
Never assume things are just going to be the way that you want them to be.

301
00:22:02,550 --> 00:22:08,070
For example if a script adds users and adding users requires root privileges then you want to check

302
00:22:08,100 --> 00:22:09,270
for root privileges.

303
00:22:09,270 --> 00:22:13,080
Don't assume that the user knows that they need to execute it with root privileges.

304
00:22:13,080 --> 00:22:15,160
They may executed as their own user.

305
00:22:15,300 --> 00:22:21,060
So you want to do some sanity checking or some idiot checking if you will to make sure that that happens

306
00:22:21,090 --> 00:22:22,310
as needed.

307
00:22:22,560 --> 00:22:27,210
And you want to do this type of sanity checking throughout the script whenever it's needed.

308
00:22:27,210 --> 00:22:31,290
Hey that brings us to the end of this lesson where you learned about some special variables that are

309
00:22:31,290 --> 00:22:32,710
created by the show.

310
00:22:32,730 --> 00:22:36,420
Now you can look up those variables by using man bash.

311
00:22:36,450 --> 00:22:41,640
You also learned how to store the output of a command in a variable using the dollar sign parentheses

312
00:22:41,640 --> 00:22:42,640
syntax.

313
00:22:42,720 --> 00:22:48,630
You also learned about how to use an if statement or and our specific case the if else statement and

314
00:22:48,630 --> 00:22:51,430
we talked a little bit about conditionals.

315
00:22:51,600 --> 00:22:56,520
You also learned that you could use help test from the command line to get some information about possible

316
00:22:56,520 --> 00:23:00,200
tests or expressions or conditions that you can check for.

317
00:23:00,210 --> 00:23:05,670
Finally you learned about some of the older syntax options such as using back text when assigning the

318
00:23:05,670 --> 00:23:11,970
output of a command to a variable and using a single bracket when doing tests versus the double bracket.
