1
00:00:03,480 --> 00:00:09,930
In a previous lesson we use the if statement in a positive form meaning if this expression is true then

2
00:00:09,930 --> 00:00:16,260
do something in this lesson we're going to use the logic of if this expression is not true then do something

3
00:00:16,740 --> 00:00:22,380
you're also going to learn how to determine if a given command failed or succeeded by checking the exit

4
00:00:22,380 --> 00:00:27,420
status of the command as well as how to control the exit status of your scripts.

5
00:00:27,450 --> 00:00:32,550
You can follow along with me right now if you'd like or you can wait for the practice exercise where

6
00:00:32,550 --> 00:00:36,330
you'll get to apply all the concepts you've learned so far.

7
00:00:36,360 --> 00:00:41,160
The first thing I'm going to do is open up a command line section of my local machine and move into

8
00:00:41,160 --> 00:00:42,420
our working folder.

9
00:00:44,260 --> 00:00:51,810
I'm here I'm going to go into the project folder that we're working on which is local users.

10
00:00:51,920 --> 00:01:03,310
Now I'm going to start the virtual machine and connect to it.

11
00:01:03,490 --> 00:01:08,890
Now that I'm inside the virtual machine I'm going to C.D into the shared directory of slash vagrant

12
00:01:09,340 --> 00:01:15,070
and I'm going to start working on this script for this lesson which is tell users which stands for a

13
00:01:15,070 --> 00:01:16,610
local user demo.

14
00:01:16,730 --> 00:01:19,920
Oh three got sh horse.

15
00:01:19,920 --> 00:01:23,370
We start out our shell scripts the exact same way every time.

16
00:01:24,490 --> 00:01:28,800
We supply the interpreter to be used on the shebang line here.

17
00:01:28,810 --> 00:01:45,700
Now we're going to specify a goal for our script.

18
00:01:45,930 --> 00:01:50,540
So this script is just going to display the ID and user name of the user that's executing the script.

19
00:01:50,610 --> 00:01:54,990
And we're also going to display to the screen if the user is the vagrant user or not.

20
00:01:54,990 --> 00:01:58,130
So we're going to test for the vagrant user.

21
00:01:58,320 --> 00:02:03,390
I'm going to go ahead and put the steps in as comments and then fill in the code as we go.

22
00:02:03,390 --> 00:02:18,010
So the first step we want to do is display the new ID.

23
00:02:18,050 --> 00:02:24,060
The next thing we're going to do is only display a message if the ID does not match 1000.

24
00:02:24,200 --> 00:02:32,370
From there we're just going to display the username then we're going to test if the username command

25
00:02:32,450 --> 00:02:42,170
succeeded from them and demonstrate how you can use a string tests conditional.

26
00:02:42,180 --> 00:02:50,240
Next we're going to test for not equal or the string.

27
00:02:50,270 --> 00:02:53,720
So let's go back to the top of our script here and start filling in some code.

28
00:02:53,720 --> 00:02:58,140
So the first thing I want to do is display the ID as you already know the U.

29
00:02:58,130 --> 00:03:01,870
ID is a shell variable that is set by default.

30
00:03:01,970 --> 00:03:07,010
So we can just display that we're going to use double quotes because we're going to have a variable

31
00:03:07,010 --> 00:03:08,570
in here that we want to get expanded

32
00:03:11,480 --> 00:03:17,480
Of course when we access a variable we access it with a dollar sign in opening curly brace the variable

33
00:03:17,480 --> 00:03:24,910
name and a closing curly brace it let's just write and quit our changes here and then execute the script

34
00:03:24,940 --> 00:03:26,700
and see what happens.

35
00:03:26,830 --> 00:03:31,390
Remember the script has to have an executable bitset on it before it can be executed so we're going

36
00:03:31,390 --> 00:03:32,630
to chmod the script.

37
00:03:36,950 --> 00:03:41,650
Now we're going to execute it with Dot Ford slash l user demo 0 3.

38
00:03:41,730 --> 00:03:47,530
SH K and displays r you ID which is 1000.

39
00:03:47,540 --> 00:03:51,530
Now let's get back to the script and continue editing our script here.

40
00:03:51,530 --> 00:03:57,500
Now what we want to do is only display a message if the ID does not match 1000.

41
00:03:57,530 --> 00:04:02,480
So we're going to have a variable called you ID to test for.

42
00:04:02,480 --> 00:04:09,380
And we're going to set that to one thousand by the way there is this concept called dry which stands

43
00:04:09,380 --> 00:04:09,610
for.

44
00:04:09,610 --> 00:04:11,240
Don't repeat yourself.

45
00:04:11,420 --> 00:04:15,620
We're going to apply that concept in this script through the use of a variable.

46
00:04:15,620 --> 00:04:21,290
We're going to reference this you id more than once so instead of hard coding that you ID in multiple

47
00:04:21,290 --> 00:04:25,970
places in the script you're going to specify that value in one place.

48
00:04:25,970 --> 00:04:30,830
If you ever want to change that you ID you only have to change it in one place and not go searching

49
00:04:31,130 --> 00:04:36,260
all throughout the entire script and change it in multiple places in a small script like we're writing

50
00:04:36,260 --> 00:04:41,510
here it might not look like that such a big deal but when you create larger more complex scripts you're

51
00:04:41,510 --> 00:04:45,180
going to be glad that you followed the dry principle.

52
00:04:45,190 --> 00:04:48,380
OK so let's test for this you idea with an if statement.

53
00:04:48,390 --> 00:04:55,360
We remember that we use the F word followed by a space double opening brackets followed by a space.

54
00:04:55,380 --> 00:05:03,090
We're going to look at the ID and we're going to see if it's actually not equal to the idea that we're

55
00:05:03,090 --> 00:05:04,020
going to test for

56
00:05:13,530 --> 00:05:24,520
if the ID is not equal to the ID to test for then we're going to display your ID does not match you

57
00:05:24,520 --> 00:05:30,240
ID that we're testing for.

58
00:05:30,390 --> 00:05:37,740
Let's say we don't want the user to go any farther unless they have the ID of 1000 in order to stop

59
00:05:37,740 --> 00:05:38,940
the execution of the script.

60
00:05:38,940 --> 00:05:44,710
We're going to use the exit command by the way we can supply an exit status after the exit command.

61
00:05:44,820 --> 00:05:47,080
We're going to supply one here.

62
00:05:47,160 --> 00:05:52,290
The reason why we're going to specify one is because this script does not successfully execute all the

63
00:05:52,290 --> 00:05:54,350
way to the bottom as it should.

64
00:05:54,510 --> 00:05:58,550
So we're going to want to exit with a non zero status.

65
00:05:58,560 --> 00:06:07,200
So by convention when a script or a program execute successfully it returns an exit status of 0 if it

66
00:06:07,200 --> 00:06:13,020
doesn't execute successfully for whatever reason it returns a non zero exit status.

67
00:06:13,020 --> 00:06:15,780
A lot of times that exit status is one.

68
00:06:16,050 --> 00:06:17,630
However it could be two.

69
00:06:17,640 --> 00:06:20,970
It could be 99 it could be 244.

70
00:06:21,030 --> 00:06:25,280
You can use a variety of different exit statuses if you want.

71
00:06:25,290 --> 00:06:27,760
Let me close out this statement I'll show you an example.

72
00:06:30,240 --> 00:06:31,970
Let me write and quit real quick.

73
00:06:32,130 --> 00:06:36,210
So let's look at a command that we're going to use We're going to be using the user add command to add

74
00:06:36,210 --> 00:06:36,830
users.

75
00:06:36,930 --> 00:06:41,040
So let's take a look at its man page.

76
00:06:41,080 --> 00:06:46,560
I'm going to search for exit status by Ford slash exit and press enter.

77
00:06:47,110 --> 00:06:54,040
So here it's called them exit values the user add command exits with the following values 0 6 success.

78
00:06:54,070 --> 00:06:58,650
You can actually test for these exit statuses and we'll get to that in just a minute.

79
00:06:58,660 --> 00:07:03,850
So for example exit status of nine means the user name is already in use.

80
00:07:04,090 --> 00:07:09,760
So if you're writing a script and you run the user add command and you get an exit status of nine then

81
00:07:09,760 --> 00:07:16,270
you can know that that user name is in use and it's not for some other reason such as an invalid command

82
00:07:16,270 --> 00:07:20,350
syntax which would be an error status of two.

83
00:07:20,350 --> 00:07:22,980
So again we're going to use the exit show built in.

84
00:07:23,050 --> 00:07:24,430
And let's get some help about that.

85
00:07:24,430 --> 00:07:28,130
Let's see what help is available for exit.

86
00:07:28,200 --> 00:07:33,180
So as you can see you can use exit without any options because the in there is in brackets and that

87
00:07:33,180 --> 00:07:34,590
means it's optional.

88
00:07:34,590 --> 00:07:40,950
So you can just type exit and exits the shell if you want to supply an exit status you just supply it

89
00:07:40,950 --> 00:07:44,430
to exit exit 1 for example like we're using in our script.

90
00:07:44,430 --> 00:07:51,030
If you omit that status then the exit status is that of the last command executed.

91
00:07:51,030 --> 00:07:54,230
Sometimes that's what you want and sometimes that's not what you want.

92
00:07:54,300 --> 00:08:00,830
So if you want to be explicit then specify a exit status following the exit shell built in.

93
00:08:00,840 --> 00:08:03,650
Let's get back to our script real quick here.

94
00:08:04,860 --> 00:08:07,290
So now we've gone over the exit status.

95
00:08:07,290 --> 00:08:10,650
So let's review where this dash in e came from.

96
00:08:10,650 --> 00:08:18,180
So if your ID is dash any which means not equal to the ID to test for then we're going to execute this

97
00:08:18,180 --> 00:08:18,970
code.

98
00:08:19,350 --> 00:08:24,040
Hopefully you remember that you can get information about these tests by running help test.

99
00:08:24,240 --> 00:08:31,050
And here we're doing a arithmetic test means we're testing numbers here we have dash Q Which corresponds

100
00:08:31,050 --> 00:08:39,570
to equal dashi and he is not equal Dasch LTE is less than Nashe L E is less than or equal G is greater

101
00:08:39,570 --> 00:08:43,430
than and dash G is greater than or equal to.

102
00:08:43,440 --> 00:08:47,460
So we're using dash any do tests for not equal.

103
00:08:47,460 --> 00:08:50,020
Now let's go ahead and executer script and see what happens.

104
00:08:53,540 --> 00:09:02,660
Says your ID is 1000 and no messages were displayed to the screen because you I.D. does not equal 1000

105
00:09:03,050 --> 00:09:05,480
is actually false.

106
00:09:05,480 --> 00:09:10,060
Now let's see what happens when we execute the script with the different user ID.

107
00:09:10,070 --> 00:09:15,140
Well one way to do that is to execute it with Sulu privileges and that will run the script with root

108
00:09:15,140 --> 00:09:15,930
user privileges.

109
00:09:15,940 --> 00:09:23,350
And as we know the root user always has the UI ID of 0.

110
00:09:23,390 --> 00:09:28,980
So this time you get your you ID is zero your ID does not match 1000.

111
00:09:29,300 --> 00:09:37,070
So the if statement evaluates to if 0 and not equal 1000 then do something which has to echo your ID

112
00:09:37,070 --> 00:09:40,660
does not match 1000 OK continue working on the script.

113
00:09:43,470 --> 00:09:45,200
Now let's display the username

114
00:09:49,900 --> 00:09:56,360
as you probably remember the dollar sign parentheses syntax is used to capture the output of a command.

115
00:09:56,390 --> 00:10:02,260
And we're going to send the output of that command and store it into the variable user underscore name.

116
00:10:04,760 --> 00:10:10,580
Now what we want to do is test if that command succeeded and how we do this of course is with an if

117
00:10:10,580 --> 00:10:11,180
statement.

118
00:10:21,820 --> 00:10:24,920
So Bash has some special variables that it sets.

119
00:10:25,120 --> 00:10:29,040
And one of these special variables is a dollar sign questionmark.

120
00:10:29,200 --> 00:10:34,420
And of course as you know you can encapsulate that and curly braces so dollar sign opening Crilley race

121
00:10:34,420 --> 00:10:41,050
questionmark closing curly brace is actually a special variable that holds the exit status of the most

122
00:10:41,050 --> 00:10:43,050
recently executed command.

123
00:10:43,240 --> 00:10:47,740
So the most recently executed command in our script is ID dash you in.

124
00:10:47,770 --> 00:10:53,250
So the exit status of that script is going to be stored in dollar question mark.

125
00:10:53,260 --> 00:10:57,290
Now we can use dollar question mark to do some comparisons here.

126
00:10:57,280 --> 00:10:58,530
Make a decision.

127
00:10:58,540 --> 00:11:05,920
So if dollar question mark does not equal zero then what we want to do is tell the person that executing

128
00:11:05,920 --> 00:11:16,850
this script that the ID command did not execute successfully and we're going to cause our script to

129
00:11:16,850 --> 00:11:24,590
exit as well as to exit 1 and then close our if statement if we get past this if statement that means

130
00:11:24,620 --> 00:11:27,350
the ID command did successfully complete.

131
00:11:27,350 --> 00:11:35,670
So we can display their username.

132
00:11:35,890 --> 00:11:41,980
So let's execute this script on the command line and also do some demonstrations where the dollar question

133
00:11:41,980 --> 00:11:48,090
marks special variable.

134
00:11:48,110 --> 00:11:52,290
So now the output of the script is your you ideas 1000 your user name is vagrant.

135
00:11:52,430 --> 00:11:58,100
And as you remember the username is a vagrant echo command was directly after the if statement.

136
00:11:58,100 --> 00:12:01,070
So the if statement did not execute.

137
00:12:01,070 --> 00:12:04,990
So that means the ID command actually executed properly.

138
00:12:05,240 --> 00:12:11,990
So let's test this out let's do id dash you in which we know is a good command to get the user name

139
00:12:11,990 --> 00:12:13,600
of the current user.

140
00:12:13,640 --> 00:12:18,650
Again the special variable dollar questionmark contains the exit code.

141
00:12:18,650 --> 00:12:19,560
Let's look at it.

142
00:12:19,560 --> 00:12:20,280
So Will do.

143
00:12:20,480 --> 00:12:28,400
Echo dollar question mark and the exit status of the ID command is zero.

144
00:12:28,400 --> 00:12:36,750
Now let's make the ID command fail in some way I happen to know that dash X is not a valid option to

145
00:12:36,750 --> 00:12:39,340
ID so let's run that command and see what happens.

146
00:12:39,690 --> 00:12:42,410
So we get an invalid option dash x.

147
00:12:42,420 --> 00:12:47,010
So now let's look at the exit status which is stored in dollar question mark.

148
00:12:47,280 --> 00:12:52,740
So now you can see that one is the exit status of that command that failed.

149
00:12:52,740 --> 00:12:58,070
So again by convention it's a non zero exit status and this case is one.

150
00:12:58,140 --> 00:13:02,040
Again it could be two it could be three it could be one hundred thirty nine.

151
00:13:02,040 --> 00:13:05,030
In our user add example that we were looking at earlier.

152
00:13:05,220 --> 00:13:11,460
So again if we want to test for success we'll probably want to test if it does not equal 0 because that's

153
00:13:11,460 --> 00:13:12,510
the convention.

154
00:13:12,870 --> 00:13:14,440
Kate let's continue with our script.

155
00:13:20,830 --> 00:13:24,670
Instead of testing numbers let's do a test against the string.

156
00:13:25,000 --> 00:13:27,370
So let's have a user name to test for

157
00:13:33,180 --> 00:13:50,010
we'll use an IF statement to perform a test.

158
00:13:50,100 --> 00:13:56,640
So this reads if the user name equals the user name to test for if that turns out to be true then what

159
00:13:56,640 --> 00:14:01,270
we want to do is echo your user name matches.

160
00:14:02,310 --> 00:14:03,720
The user name that we're testing for

161
00:14:09,580 --> 00:14:16,240
it's important to point out that the equal sign can either be an assignment operator or a test operator

162
00:14:16,480 --> 00:14:18,470
depending on the context.

163
00:14:18,490 --> 00:14:24,720
If you look at the variable assignment user name to test for that equal sign is used as assignment where

164
00:14:24,730 --> 00:14:29,600
assigning the value of vagrant into the value of user name to test for.

165
00:14:29,620 --> 00:14:34,720
However when we're doing a test inside these double brackets then it's a test operator.

166
00:14:34,720 --> 00:14:41,830
It's not assigning user name to test for to user name it's comparing them it's testing them by the way

167
00:14:41,830 --> 00:14:45,870
sometimes you'll see a double equal sign and let me put this in the script to show you here.

168
00:14:48,110 --> 00:14:53,780
So sometimes you will see that when you use the double equal sign operator the string to the right of

169
00:14:53,780 --> 00:14:59,060
the operator is used as a pattern and pattern matching is performed.

170
00:14:59,360 --> 00:15:05,790
So we're not looking for a pattern we're looking for an exact match so we'll just use one equal sign.

171
00:15:05,780 --> 00:15:11,510
However some people even use the double equal signs when they don't use a different pattern if they're

172
00:15:11,690 --> 00:15:13,460
also using an exact match.

173
00:15:13,460 --> 00:15:18,380
So be aware that sometimes you'll see a single equal sign and sometimes you will see a double equal

174
00:15:18,380 --> 00:15:18,920
sign.

175
00:15:18,950 --> 00:15:21,780
We're going to stick with a single equal sign in this case.

176
00:15:24,320 --> 00:15:27,880
OK let's exit over script and see what happens when we execute it.

177
00:15:27,880 --> 00:15:28,160
Now

178
00:15:33,260 --> 00:15:34,100
those are you ideas.

179
00:15:34,100 --> 00:15:37,580
Thousand are you using name is vagrant and our user name matches Veiga.

180
00:15:37,640 --> 00:15:39,200
Well let's run this with Sue

181
00:15:42,270 --> 00:15:44,040
are you ID is 0.

182
00:15:44,080 --> 00:15:46,340
Your I.D. does not match 1000.

183
00:15:46,350 --> 00:15:51,570
And again if you remember we had an exit statement in our script and we exit it as we want.

184
00:15:51,570 --> 00:15:54,440
So let's check the exit status and see if that's true.

185
00:16:00,930 --> 00:16:04,990
And sure enough the exit status of our script is one.

186
00:16:05,260 --> 00:16:08,530
Hey let's get back to our script and finish it up.

187
00:16:14,580 --> 00:16:17,880
Now let's test for inequality using an if statement with a string.

188
00:16:17,880 --> 00:16:20,520
So we'll do if double brackets

189
00:16:23,390 --> 00:16:32,380
username is not equal to user name to test for.

190
00:16:32,520 --> 00:16:43,880
Then we're going to echo your user name does not match user name to test for and then we're going to

191
00:16:43,880 --> 00:16:49,620
exit with one and we're going to end the statement with ephi.

192
00:16:49,660 --> 00:16:56,200
And finally if we make it all the way to the end of the script we can use exit and we can be very explicit

193
00:16:56,200 --> 00:17:02,320
about the exit status that we want to use we'll use 0 to say our script completed successfully.

194
00:17:02,320 --> 00:17:08,350
Remember that if you don't specify an exit status or exit code the exit status of the most recently

195
00:17:08,380 --> 00:17:15,100
previously executed command will be used as the exit status of your script or as the exit status of

196
00:17:15,100 --> 00:17:16,270
the exit command.

197
00:17:16,630 --> 00:17:17,980
Hey that wraps it up for this script.

198
00:17:17,980 --> 00:17:21,020
Let's save our changes and execute at the command line.

199
00:17:25,060 --> 00:17:30,170
So you can see the output on the screen and let's check our exit status here.

200
00:17:30,230 --> 00:17:35,660
We have an exit status of 0 which means by convention that our script completed successfully.
