1
00:00:03,240 --> 00:00:10,050
Welcome to this exercise walk through now the script you're going to write today is very closely based

2
00:00:10,050 --> 00:00:11,910
on the previous script you wrote.

3
00:00:12,090 --> 00:00:16,760
As a matter of fact we don't have to do any new coding if we don't want to.

4
00:00:16,830 --> 00:00:23,290
You can simply update that existing script to conform to the new requirements that you have now.

5
00:00:23,460 --> 00:00:29,010
And speaking of those new requirements the helpdesk in our scenario has been using your script and they

6
00:00:29,010 --> 00:00:33,110
notice that there are some extra output that they don't need that they don't care to see.

7
00:00:33,110 --> 00:00:34,860
It really doesn't make any sense to them.

8
00:00:35,010 --> 00:00:37,950
So they want you to make that output disappear.

9
00:00:38,070 --> 00:00:41,550
And of course you know that output comes from the password command.

10
00:00:41,750 --> 00:00:42,690
Ss.

11
00:00:43,050 --> 00:00:49,350
And that you're going to have to use IO redirection to redirect that output to the null device.

12
00:00:49,430 --> 00:00:54,690
And so you start to think about what else you can do to this script with IO redirection and you come

13
00:00:54,690 --> 00:01:01,080
up with that you could actually conform to standard conventions standard Linux programming conventions

14
00:01:01,410 --> 00:01:05,880
by redirecting error messages to standard error and so on.

15
00:01:05,880 --> 00:01:11,700
So with that in mind let's look at just a couple of changes that we're going to make with this script.

16
00:01:11,910 --> 00:01:18,990
So we're going to change the name of the script from add new local user to add newer local user.

17
00:01:18,990 --> 00:01:23,640
Now in the real world I wouldn't necessarily change the name I would just make these couple of updates

18
00:01:23,640 --> 00:01:28,190
to that script in place of course tested first and then put it in for action.

19
00:01:28,200 --> 00:01:33,480
However just for the sake of distinguishing what exercise in what script you're working on if you need

20
00:01:33,480 --> 00:01:34,580
help and so on.

21
00:01:34,740 --> 00:01:40,590
I'm just going to use a new name here so that we just get a unique script for each exercise that we're

22
00:01:40,590 --> 00:01:41,360
working on.

23
00:01:41,550 --> 00:01:45,200
So again in the real world you can keep the same name if you'd like.

24
00:01:45,210 --> 00:01:49,290
Now really all the other requirements are still in play like you want to make sure that the script is

25
00:01:49,290 --> 00:01:54,690
run with super user privileges but the only difference there is that when you display some help or an

26
00:01:54,690 --> 00:02:00,930
error message when they don't then what you want to do is send that output to standard error instead

27
00:02:00,930 --> 00:02:03,110
of standard output like we've been doing.

28
00:02:03,360 --> 00:02:08,100
And that's going to be the same thing for all the error statuses that we're going to display in our

29
00:02:08,100 --> 00:02:11,970
scripts so that's really the main thing that we're going to be changing here.

30
00:02:12,000 --> 00:02:17,190
So here I am on my local system with my terminal open and I'm going to navigate to the class folder

31
00:02:19,280 --> 00:02:23,960
from where I'm going to navigate to the vagrant project that we're using for this set of scripts and

32
00:02:23,960 --> 00:02:25,900
that's local users.

33
00:02:26,090 --> 00:02:30,050
And then I'm going to bring this virtual machine up and then connect to it.

34
00:02:34,590 --> 00:02:41,510
Now I'm going to move into the shared folder of forward slash vagrant and then here I'm actually going

35
00:02:41,510 --> 00:02:48,410
to copy the previous script we used and just copy it to this new name that we're going to work on today.

36
00:02:50,830 --> 00:02:55,660
Of course you can write this entire script from scratch again if you would like just to give yourself

37
00:02:55,660 --> 00:02:56,860
some extra practice.

38
00:02:57,000 --> 00:03:01,540
But here for the walk through I'm just going to show you a couple of changes that you need to make from

39
00:03:01,540 --> 00:03:06,650
the previous script in order to conform to these new requirements that we've come up with here.

40
00:03:10,180 --> 00:03:14,770
So what I'm going to do is just start with the very top of the script and work my way down looking for

41
00:03:14,800 --> 00:03:17,020
any changes that I need to make.

42
00:03:17,020 --> 00:03:22,200
So here I see an exit status of one and an echo statement previously before.

43
00:03:22,210 --> 00:03:25,850
So that's really our error message when we're exiting with a 1.

44
00:03:26,050 --> 00:03:32,390
So what we need to do is send this output from the echo command to standard air right now by default

45
00:03:32,470 --> 00:03:35,750
it's going to be displayed to standard output.

46
00:03:35,910 --> 00:03:41,000
And so we can do this with a greater than symbol and ampersand and 2.

47
00:03:41,080 --> 00:03:45,680
Now realize here that the greater than symbol assumes standard output.

48
00:03:45,880 --> 00:03:52,420
So it's also the same as this if we want to be explicit and specify how far the descriptor one for standard

49
00:03:52,420 --> 00:03:53,120
output.

50
00:03:53,320 --> 00:03:54,070
I'm not going to do that.

51
00:03:54,070 --> 00:03:59,270
I'm just going to delete that and then use this syntax The greater than ampersand too.

52
00:03:59,320 --> 00:04:06,100
And of course as you know in ampersand right after a redirection operator implies that you're using

53
00:04:06,100 --> 00:04:08,380
a file descriptor instead of a file name.

54
00:04:08,380 --> 00:04:12,520
So again file descriptor 2 is associated with standard air.

55
00:04:12,670 --> 00:04:18,380
So that makes the standard output here the Echo would generate be displayed on standard air.

56
00:04:18,600 --> 00:04:20,900
Going to keep working my way down here.

57
00:04:21,010 --> 00:04:25,420
Here I come to another exit statement here and I'm just going to do the same thing.

58
00:04:25,420 --> 00:04:28,290
We're just going to send all this output to standard error

59
00:04:31,400 --> 00:04:35,690
window redraw the screen with the cursor at the top and then just keep on working down here.

60
00:04:36,540 --> 00:04:40,410
So assigning variables is not going to cause any output.

61
00:04:40,410 --> 00:04:46,480
The shift command won't either the output of this command is stored in a variable so it's not getting

62
00:04:46,480 --> 00:04:47,880
displayed anywhere.

63
00:04:48,220 --> 00:04:52,450
OK this is a command that could possibly generate output.

64
00:04:52,460 --> 00:04:59,140
Now let me go down the script a little bit here you can see that we're actually checking for its exit

65
00:04:59,140 --> 00:05:00,460
status down here.

66
00:05:00,580 --> 00:05:05,190
So we don't necessarily need to see the output generated by this command.

67
00:05:05,200 --> 00:05:08,550
So actually what I'm going to do here is ignore all the output.

68
00:05:08,560 --> 00:05:12,920
I don't need to see the standard output or the standard error from this command.

69
00:05:14,580 --> 00:05:20,940
So to redirect both standard output and standard error will use ampersand greater than sign and then

70
00:05:20,940 --> 00:05:26,000
we're going to send that to the dev null device and that's just going to throw away anything and it

71
00:05:26,000 --> 00:05:32,320
gets so all the output generated by this command will not be displayed to the user running the script.

72
00:05:32,500 --> 00:05:37,840
Again we come to another exit statement and that means this echo command right above it is going to

73
00:05:37,840 --> 00:05:46,520
be an error message so we're going to send this to standard air.

74
00:05:46,520 --> 00:05:50,580
OK so this password command we know generates some output.

75
00:05:50,720 --> 00:05:56,810
So what we want to do is you know send that output to dev null so the user doesn't see this output.

76
00:05:56,810 --> 00:06:02,360
So again we're going to use ampersand greater than and send that to the dev null device.

77
00:06:02,420 --> 00:06:07,280
We're doing a check for an exit status of the password command and then we're going to display an error

78
00:06:07,280 --> 00:06:08,390
if we get an error.

79
00:06:08,480 --> 00:06:15,980
So that means this is an error message and needs to be displayed on standard air.

80
00:06:15,980 --> 00:06:18,830
Now we have a another password command again.

81
00:06:18,920 --> 00:06:24,400
The helpdesk users don't want to see these messages so we're just going to send all that output to dev

82
00:06:24,400 --> 00:06:29,770
null.

83
00:06:29,930 --> 00:06:36,950
Since we haven't generated any output in our script here there's no need for a blank line to separate

84
00:06:37,010 --> 00:06:41,920
the information that we're going to display for the user between the output of the previous commands.

85
00:06:41,930 --> 00:06:48,350
So I'm just going to get rid of this echo statement and then the rest of our script here generates the

86
00:06:48,350 --> 00:06:50,780
output that we want the user to see.

87
00:06:50,780 --> 00:06:55,520
So let's go ahead and Saveur changes and then we'll check the permissions on the script.

88
00:06:55,520 --> 00:07:01,190
Now when you copy a script it gets the same permissions as the script it was copied from but we'll go

89
00:07:01,190 --> 00:07:04,580
ahead and check the permissions here just to be thorough.

90
00:07:06,260 --> 00:07:07,820
Hey it's 755.

91
00:07:07,820 --> 00:07:08,620
So that looks good.

92
00:07:08,620 --> 00:07:10,090
You can see the x's there.

93
00:07:10,280 --> 00:07:14,840
And all three sets of permissions so anyone can execute the script.

94
00:07:15,550 --> 00:07:19,330
Now obviously if they don't run it to privileges they're going to get an error message.

95
00:07:19,370 --> 00:07:21,680
But again the executable bit is there.

96
00:07:22,070 --> 00:07:27,790
If you created this script from scratch then you're going to need a chmod 755 your script.

97
00:07:27,860 --> 00:07:33,500
So now that we have our changes in place or a new script written let's go ahead and test it.

98
00:07:33,500 --> 00:07:40,250
So we're going to let's add a new user here and we'll just call this username Turing and the comment

99
00:07:40,250 --> 00:07:48,060
will be Alan Turing and hit enter So now you can see that the only thing is displayed is the username

100
00:07:48,090 --> 00:07:51,350
the password and the host that this user was created on.

101
00:07:51,360 --> 00:07:55,010
None of that extra output from the password commands were displayed on the screen.

102
00:07:55,050 --> 00:07:57,680
And this is exactly what we're looking for.

103
00:07:57,900 --> 00:08:03,330
So going to go ahead and create our other users here we have was

104
00:08:06,830 --> 00:08:13,010
again the display is just the information that is needed by the people running the command will create

105
00:08:13,010 --> 00:08:14,180
another user here.

106
00:08:14,210 --> 00:08:18,210
Call this more for Gordon Moore.

107
00:08:18,320 --> 00:08:20,160
Kate we've created three accounts.

108
00:08:20,360 --> 00:08:26,330
Now let's go ahead and see if those accounts were added to the ETSI password file.

109
00:08:26,360 --> 00:08:31,700
We can do that by just telling the ANSI password file are actually going to do a dash 3 for the last

110
00:08:31,730 --> 00:08:34,190
three lines of the ETSI password file.

111
00:08:34,400 --> 00:08:34,610
OK.

112
00:08:34,610 --> 00:08:35,580
There you can see our users.

113
00:08:35,580 --> 00:08:37,850
Turing was and more.

114
00:08:37,850 --> 00:08:43,010
Now I'm just going to switch to an account to be thorough here to make sure that it still forces the

115
00:08:43,010 --> 00:08:46,830
password expiration and prompts us for a new password on log in.

116
00:08:47,060 --> 00:08:52,290
As a matter of fact before I do this I need to go back up here and actually copy the password

117
00:08:58,010 --> 00:09:01,500
and asking for a current password and then a new password

118
00:09:09,000 --> 00:09:10,540
OK that looks like it worked.

119
00:09:10,540 --> 00:09:16,960
And we're all done with this user so we'll just hit exit here and return to our normal vagrant user.

120
00:09:16,960 --> 00:09:21,940
So the core functionality of our script is still in place it creates users that expire passwords and

121
00:09:21,940 --> 00:09:22,750
so on.

122
00:09:22,750 --> 00:09:26,550
So what we want to do is actually test our new functionality.

123
00:09:26,980 --> 00:09:33,010
So one way to do that is to make our script generate an air and we can do that by not running it with

124
00:09:33,070 --> 00:09:34,500
super user privileges.

125
00:09:36,690 --> 00:09:38,730
Hey we get a message on our screen.

126
00:09:38,860 --> 00:09:43,510
Of course you can't tell if that message is standard input or standard output but we'll get to that

127
00:09:43,510 --> 00:09:44,660
in just a second.

128
00:09:44,800 --> 00:09:46,630
It prints a message to our screen.

129
00:09:46,630 --> 00:09:49,150
Now let's check the exit status of the command.

130
00:09:51,750 --> 00:09:53,930
Sure enough it's an exit status of one.

131
00:09:54,090 --> 00:09:56,460
Which is exactly what we want.

132
00:09:56,490 --> 00:10:01,890
Now let's do the same thing again but this time let's redirect the standard output and standard error

133
00:10:01,890 --> 00:10:05,870
to different places and then check those to see what is going where.

134
00:10:05,940 --> 00:10:11,520
Now because by default standard output and standard error are both displayed to our screen we can't

135
00:10:11,520 --> 00:10:16,450
necessarily tell what standard output or standard error just by looking.

136
00:10:16,800 --> 00:10:22,600
So if we're going to do is redirect standard output to a file called STV out we can name that anything.

137
00:10:22,600 --> 00:10:25,740
I'm just going to use the kind of standard abbreviation here.

138
00:10:25,740 --> 00:10:28,130
Again standard error is going to go to a file.

139
00:10:28,140 --> 00:10:30,040
I'm just going to name SDD.

140
00:10:30,090 --> 00:10:33,600
E r r again you can name it any file name you want.

141
00:10:33,600 --> 00:10:36,710
So all the output either goes to standard out or standard error.

142
00:10:36,870 --> 00:10:39,560
So let's examine the standard elf file.

143
00:10:40,050 --> 00:10:41,960
And there's nothing in that file.

144
00:10:42,240 --> 00:10:46,720
And if we look at standard air then our error messages in that file.

145
00:10:46,800 --> 00:10:52,380
So we know that the echo statement is displaying its output to standard error and that's exactly what

146
00:10:52,380 --> 00:10:53,420
we want.

147
00:10:53,430 --> 00:10:56,100
So let's go ahead and remove these files.

148
00:10:57,200 --> 00:11:00,820
Let's test for another error condition that our script checks for.

149
00:11:01,040 --> 00:11:06,460
We'll actually run the script here but we won't supply any arguments so will hit enter.

150
00:11:06,680 --> 00:11:11,260
And sure enough we get an exit status here of one.

151
00:11:11,450 --> 00:11:13,550
And now let's run the same kind of test.

152
00:11:13,550 --> 00:11:19,950
We'll just send the standard output to standard out and the standard air to a standard error file.

153
00:11:20,030 --> 00:11:22,660
We'll look to see if it generated in the standard output.

154
00:11:22,670 --> 00:11:23,950
It did not.

155
00:11:23,960 --> 00:11:30,500
So all the error messages went to standard error and that's exactly what we want our script to do.

156
00:11:30,610 --> 00:11:32,330
We'll go ahead and clean up here.

157
00:11:34,180 --> 00:11:34,410
All right.

158
00:11:34,420 --> 00:11:39,820
Now that brings us to the end of this exercise so we've made our changes our error messages are now

159
00:11:39,820 --> 00:11:45,430
being displayed to standard air instead of standard out and all that output that was generated from

160
00:11:45,430 --> 00:11:49,120
those other commands are now hidden from the user.

161
00:11:49,150 --> 00:11:53,730
Since I'm done for the day I'm going to exit out of the virtual machine and then go ahead and halt it.
