1
00:00:03,150 --> 00:00:09,810
Before we can cover redirecting standard error we need to talk about file descriptors a file descriptor

2
00:00:09,810 --> 00:00:15,930
is simply a number that represents an open file for us humans it's easier for us to reference files

3
00:00:15,930 --> 00:00:20,970
by name but it's easier for computers to reference them by number by default.

4
00:00:21,060 --> 00:00:24,860
Every new process starts with three open file descriptors.

5
00:00:25,020 --> 00:00:31,170
They are file descriptors zero which is standard input file descriptor one which is standard output

6
00:00:31,470 --> 00:00:35,040
and file descriptor 2 which is standard error.

7
00:00:35,040 --> 00:00:41,040
You may be thinking hold on by default standard input comes from my keyboard but my keyboard isn't a

8
00:00:41,040 --> 00:00:47,470
file and by default standard output and standard error are displayed to my screen but my screen isn't

9
00:00:47,490 --> 00:00:48,840
a file either.

10
00:00:48,840 --> 00:00:54,390
On one level that's true but Linux represents practically everything as a file.

11
00:00:55,110 --> 00:01:00,810
Perhaps a more accurate description of a file descriptor is that it's a way that a program interacts

12
00:01:00,810 --> 00:01:05,420
with files or to other resources that work like files.

13
00:01:05,610 --> 00:01:10,100
These other resources include devices such as keyboards terminals and so on.

14
00:01:10,110 --> 00:01:15,870
This abstraction of treating almost everything like a file allows you to do some really powerful things

15
00:01:15,870 --> 00:01:21,300
like take the standard output of one command that would normally be displayed to your screen and use

16
00:01:21,300 --> 00:01:23,400
it as input to another command.

17
00:01:23,400 --> 00:01:29,340
Of course I'm talking about pipes in this case so file descriptors are like pointers to sources of data

18
00:01:29,640 --> 00:01:34,260
or places that data can be written things like keyboards file screens and so on.

19
00:01:34,890 --> 00:01:42,480
OK back to I O so far in this lesson we've been using the implicit way of redirecting input and output.

20
00:01:42,570 --> 00:01:48,860
For example when we redirect standard input into a command file descriptor zero is assumed.

21
00:01:49,080 --> 00:01:55,970
Let's look at the contents of a file on our system.

22
00:01:56,020 --> 00:02:00,820
Now let's use that file as standard input let's say to the read command.

23
00:02:00,850 --> 00:02:07,850
So we're going to read into the variable x the contents of Etsy Santos release.

24
00:02:07,900 --> 00:02:12,910
Now if we echo dollar sign X we'll see the contents of that file.

25
00:02:12,910 --> 00:02:15,410
So sure enough that input redirection work.

26
00:02:15,490 --> 00:02:17,370
And that's the implicit way to do it.

27
00:02:17,620 --> 00:02:23,980
If you want to be explicit specify the file descriptor number right before the redirection operator.

28
00:02:24,070 --> 00:02:35,290
So the previous redirection is the same thing is this read x 0 less than sign Etsy Santos release and

29
00:02:35,290 --> 00:02:36,160
hit enter.

30
00:02:36,340 --> 00:02:39,720
And if we echo X sure enough it does the same thing.

31
00:02:39,760 --> 00:02:45,670
The reason it's the same is because if you don't supply a file descriptor then zero is assumed for standard

32
00:02:45,760 --> 00:02:46,780
input.

33
00:02:46,780 --> 00:02:49,990
Notice that there is no space between the file descriptor zero.

34
00:02:49,990 --> 00:02:56,720
In this case and the redirection operator the less than sine in this case this is very very important.

35
00:02:56,740 --> 00:03:01,390
If you leave a space then what you thought was a file descriptor number that you were specifying ends

36
00:03:01,390 --> 00:03:06,070
up being an argument to the command before the redirection operator so quickly.

37
00:03:06,070 --> 00:03:07,330
This is wrong.

38
00:03:07,330 --> 00:03:12,820
Read x 0 with a space when you intend to do redirection

39
00:03:16,320 --> 00:03:17,890
and we should see an error.

40
00:03:17,980 --> 00:03:23,920
And sure enough 0 is not a valid identifier that is actually coming from the read command it thinks

41
00:03:23,920 --> 00:03:30,070
we specified read x 0 then let's give it some bogus data here and then hit enter.

42
00:03:30,130 --> 00:03:32,020
And sure enough we get the same situation.

43
00:03:32,020 --> 00:03:34,060
0 is not a valid identifier.

44
00:03:34,330 --> 00:03:43,740
However this is correct read x 0 with outer space less than sine Etsy Santos release.

45
00:03:43,780 --> 00:03:47,750
Now if we look at the value of x sure enough it's assigned to the contents of that file.

46
00:03:48,190 --> 00:03:51,330
Let's be explicit with standard output redirection.

47
00:03:51,400 --> 00:03:52,500
First let's do this.

48
00:03:52,510 --> 00:03:55,710
Echo you I.D. to the file.

49
00:03:55,720 --> 00:03:59,140
Let's call it you I.D. the contents of that file.

50
00:03:59,140 --> 00:04:01,270
Sure enough 1000 that's her you.

51
00:04:01,390 --> 00:04:02,230
No big deal.

52
00:04:02,230 --> 00:04:03,970
So that was the implicit way.

53
00:04:04,090 --> 00:04:05,830
If no file descriptor is given.

54
00:04:05,860 --> 00:04:11,680
Then file descriptor 1 is assumed when using the greater than symbol the previous command is the exact

55
00:04:11,740 --> 00:04:13,420
same thing as this.

56
00:04:13,450 --> 00:04:20,080
If we specify a 1 as a file descriptor right before the greater than sign it enter and if we look at

57
00:04:20,080 --> 00:04:25,330
the contents of that file it's the same thing because those two forms of redirection are the same.

58
00:04:25,540 --> 00:04:29,030
Again do not use a space when supplying a file descriptor.

59
00:04:29,050 --> 00:04:32,510
Let's see what happens when you do that with the Echo command here.

60
00:04:32,590 --> 00:04:36,490
So we'll do your I.D. and then 1 and specify space.

61
00:04:36,490 --> 00:04:38,100
Now this is the wrong way to do this.

62
00:04:38,140 --> 00:04:39,520
Just want to make that clear.

63
00:04:39,520 --> 00:04:43,370
Now let's use the greater than symbol to the UI file.

64
00:04:43,420 --> 00:04:49,090
So now if we catch the contents of your I.D. we see we get one thousand which is what the UI variable

65
00:04:49,090 --> 00:04:55,440
expands to and 1 because one was treated as an argument to echo.

66
00:04:55,480 --> 00:05:01,410
So if we do this without redirection we'll see that that is what is displayed to our screen by convention

67
00:05:01,420 --> 00:05:08,140
normal program output is sent to standard output while error messages are sent to standard error when

68
00:05:08,140 --> 00:05:12,820
you're working at the command prompt you may not even notice the difference because again by default

69
00:05:12,850 --> 00:05:15,180
they are both displayed to the screen.

70
00:05:15,190 --> 00:05:17,980
Let's make a program generate an error.

71
00:05:18,040 --> 00:05:20,530
First let's get some more information about the head command.

72
00:05:21,790 --> 00:05:28,100
If we read the synopsis we see that file is in brackets and there's also three periods or an ellipsis

73
00:05:28,450 --> 00:05:34,810
after that which means we can supply multiple files to the head command and here's what that looks like.

74
00:05:34,840 --> 00:05:41,710
We'll just do head dash in one to read the first line of the Etsy password file and we'll also read

75
00:05:41,710 --> 00:05:44,330
the first line of the Etsy hosts file.

76
00:05:44,860 --> 00:05:51,010
So when we do this head tells us which follows displaying what lines from shows the password file the

77
00:05:51,010 --> 00:05:55,470
first line from that then it shows Etsy hosts file and the first line from that.

78
00:05:55,480 --> 00:05:58,150
Now let's use a file that doesn't exist.

79
00:05:58,450 --> 00:06:03,010
So we'll supply something we'll name it fake file and hit enter.

80
00:06:03,160 --> 00:06:08,950
So as says had cannot open forward slash fake file for reading there's no such file a directory even

81
00:06:08,950 --> 00:06:14,230
though you can't tell by looking at the output there is both standard output and standard error generated

82
00:06:14,230 --> 00:06:14,980
here.

83
00:06:14,980 --> 00:06:21,550
Let's redirect standard output to a file run the same command the greater than sign.

84
00:06:21,560 --> 00:06:27,100
We'll call this head dot out since standard error was not redirected.

85
00:06:27,100 --> 00:06:32,950
It was sent to the screen and of course that error message is that file cannot be found and the normal

86
00:06:32,950 --> 00:06:36,310
standard output is saved into this file red dot out.

87
00:06:37,000 --> 00:06:43,090
Let's redirect standard error and the way to do that is to specify its file descriptor of 2.

88
00:06:43,210 --> 00:06:47,070
So we use the same command head dash in one Etsy password.

89
00:06:47,240 --> 00:06:54,770
It's a host this file that doesn't exist and then specify a file descriptor too without any spaces.

90
00:06:54,790 --> 00:06:59,660
We'll use the greater than sign and then give a file to redirect this standard error to.

91
00:06:59,740 --> 00:07:03,720
We'll just call this head dot e r r.

92
00:07:03,910 --> 00:07:09,510
So now what comes to our screen is the standard output while the standard error goes to the file.

93
00:07:09,550 --> 00:07:10,980
So let's look at the file.

94
00:07:12,810 --> 00:07:13,490
Okay.

95
00:07:13,510 --> 00:07:15,520
That's where the error message went.

96
00:07:15,550 --> 00:07:19,920
Let's go ahead and get rid of these files for a quick head dot out and head Dot.

97
00:07:19,930 --> 00:07:27,610
E r r we can even get real fancy here and redirect standard output to one file while redirecting standard

98
00:07:27,610 --> 00:07:28,780
error to another.

99
00:07:29,080 --> 00:07:31,980
So let's do this head dash in one Etsy password.

100
00:07:31,990 --> 00:07:35,180
Etsy hosts some file that doesn't exist.

101
00:07:35,290 --> 00:07:40,890
We'll redirect standard output to head out and standard error to head our art.

102
00:07:41,520 --> 00:07:43,900
OK no output was displayed to the screen.

103
00:07:43,930 --> 00:07:49,120
Let's look at the standard output because it's in this file and now we can look at the standard error

104
00:07:49,150 --> 00:07:51,730
because it's saved in this other file.

105
00:07:51,820 --> 00:07:55,330
You can probably see that this could be helpful in some situations.

106
00:07:55,570 --> 00:08:00,040
If you were just concerned about errors that were being generated well you could redirect all the error

107
00:08:00,040 --> 00:08:05,950
messages coming from standard error into a file for example by the way appending output is probably

108
00:08:05,950 --> 00:08:10,040
what you would expect using to greater than signs assume standard out.

109
00:08:10,120 --> 00:08:15,710
But even when you use a specific file descriptor such as file descriptor 2 for standard error it still

110
00:08:15,710 --> 00:08:16,760
opens to the file.

111
00:08:17,990 --> 00:08:20,410
So let's execute our command again here.

112
00:08:20,480 --> 00:08:25,850
What we're going to change is adding the greater than symbol so we have to greater than greater than.

113
00:08:25,850 --> 00:08:32,570
And then a path to a file so we'll hit enter and let's do this again will hit enter a couple other times

114
00:08:32,570 --> 00:08:35,830
here and then catch the contents of head dot E R.

115
00:08:36,230 --> 00:08:41,360
And sure enough we have several repetitions of the error message because we repeated that file multiple

116
00:08:41,360 --> 00:08:48,140
times using append instead of overwrite what if you want to send standard output and standard error

117
00:08:48,140 --> 00:08:49,770
to the same place.

118
00:08:49,790 --> 00:08:53,020
There are a couple of different ways to accomplish this.

119
00:08:53,060 --> 00:08:58,450
I'm going to show you the older syntax first and then the new syntax right after that.

120
00:08:58,580 --> 00:09:00,940
So again we'll just use our example command here.

121
00:09:05,480 --> 00:09:12,410
We're going to send the output to head up both to greater than Ampersand 1 and hit enter.

122
00:09:12,440 --> 00:09:19,370
So if I can head both you're going to see the standard output and the standard error both in that one

123
00:09:19,370 --> 00:09:20,050
file.

124
00:09:20,180 --> 00:09:22,070
So why does that work.

125
00:09:22,340 --> 00:09:29,060
Well first off you understand that greater than sine head both is a redirecting standard output to the

126
00:09:29,060 --> 00:09:30,320
head that both file.

127
00:09:30,410 --> 00:09:36,410
That's still the same a number that comes directly before a redirect operator is a file descriptor so

128
00:09:36,680 --> 00:09:40,180
to greater than means redirect standard error.

129
00:09:40,430 --> 00:09:46,370
The new piece is the ampersand symbol normally with the redirection of file follows the redirection

130
00:09:46,370 --> 00:09:47,240
operator.

131
00:09:47,240 --> 00:09:52,530
However if you want to use a file descriptor instead of a file name use the ampersand symbol.

132
00:09:53,060 --> 00:09:59,030
So instead of redirecting standard error to a file it is being redirected to standard input.

133
00:09:59,030 --> 00:10:04,560
If you were to omit the ampersand then one would be treated as a file named one.

134
00:10:04,910 --> 00:10:10,220
So the command you see here on your screen head dash in one NC password and he hosts fake file greater

135
00:10:10,220 --> 00:10:17,760
than header both to greater than Ampersand one sends the standard output of head to the file name head

136
00:10:17,880 --> 00:10:24,770
both and append the standard error to the standard output since Standard Error is redirected to standard

137
00:10:24,890 --> 00:10:32,870
output and standard output is redirected to head that both all output will be written to head both.

138
00:10:32,930 --> 00:10:36,620
I'd like to point out here that spacing is important as well.

139
00:10:36,620 --> 00:10:40,500
Do not use a space after the redirection operator and the ampersand.

140
00:10:40,950 --> 00:10:44,480
OK let's get rid of this file head dot both.

141
00:10:44,720 --> 00:10:48,550
If that seems a little confusing and hard to follow or too complicated.

142
00:10:48,560 --> 00:10:49,580
No problem.

143
00:10:49,580 --> 00:10:51,180
You're not alone.

144
00:10:51,230 --> 00:10:57,170
Since this is a common practice new syntax was added to bash to redirect both standard input and standard

145
00:10:57,170 --> 00:11:02,400
output that syntax is Ampersand greater than and then a path to a file.

146
00:11:02,480 --> 00:11:09,650
So let's do our same command here had dash one couple of files that actually exist and one that doesn't.

147
00:11:09,650 --> 00:11:15,860
And then we're going to use this new syntax Ampersand greater than sine and there's no spaces there

148
00:11:16,040 --> 00:11:21,560
and then provide a path to a file and that path is just going to be our current directory at head dot

149
00:11:21,590 --> 00:11:22,850
both.

150
00:11:22,850 --> 00:11:26,570
That's much prettier and probably easier to understand in my opinion.

151
00:11:26,650 --> 00:11:28,060
And if we look at head both.

152
00:11:28,070 --> 00:11:32,690
Sure enough the standard output and standard error both end up there.

153
00:11:33,110 --> 00:11:38,050
Again using double greater than signs appends to the file so let's prove that real quick.

154
00:11:38,150 --> 00:11:39,170
We'll just do this.

155
00:11:39,170 --> 00:11:43,550
Adding other greater than sign here so it's Ampersand greater than greater than that.

156
00:11:43,580 --> 00:11:44,360
Enter.

157
00:11:44,420 --> 00:11:51,290
And now if we cat had both we'll see two sections of output because we've ran the command twice and

158
00:11:51,290 --> 00:11:53,490
the second time we appended to head.

159
00:11:53,540 --> 00:11:53,930
Both

160
00:11:57,280 --> 00:12:02,620
if you think back to when you first started using pipes you learned that a pipe takes the standard output

161
00:12:02,620 --> 00:12:07,150
of one command and uses it as standard input for another command.

162
00:12:07,150 --> 00:12:10,730
This means that standard error doesn't flow through the pipe.

163
00:12:10,750 --> 00:12:12,570
Let's demonstrate this first.

164
00:12:12,670 --> 00:12:14,950
Let's take a look at the cat man page

165
00:12:19,290 --> 00:12:22,320
want to point out this dash in option that numbers.

166
00:12:22,350 --> 00:12:31,430
All output lines so if we go back to our head command here without any redirection it looks like it

167
00:12:31,580 --> 00:12:33,460
specifies six lines.

168
00:12:33,590 --> 00:12:40,480
That's the password routes a blank line as he hosts one that starts with a 127 and then heads 1 2 3

169
00:12:40,520 --> 00:12:42,530
4 5 6.

170
00:12:42,530 --> 00:12:48,890
However if we pipe this to cat and use the dash in option Kat only counts five lines.

171
00:12:48,890 --> 00:12:54,730
You see first standard error is displayed to the screen because it was not passed through the pipe.

172
00:12:54,800 --> 00:13:00,170
The standard output of the head command was passed through the pipe as the input to the cat command.

173
00:13:00,170 --> 00:13:04,370
The cat command numbered each file it received a standard input.

174
00:13:04,370 --> 00:13:09,050
It's important to know that about pipes that you're only getting the standard output going through the

175
00:13:09,050 --> 00:13:09,800
pipe.

176
00:13:09,800 --> 00:13:14,210
Now this could be exactly the way you want things to work when you're working with pipes or it might

177
00:13:14,210 --> 00:13:15,170
not be.

178
00:13:15,350 --> 00:13:20,210
If you want to force all the output of a command through the pipe then you need to append the standard

179
00:13:20,240 --> 00:13:23,540
error to the standard input.

180
00:13:23,540 --> 00:13:30,910
So one way we can do this is specifying a file descriptor of 2 which is for standard error and then

181
00:13:30,930 --> 00:13:38,060
redirect that into Ampersand 1 which represents file descriptor 1 which in turn represents a standard

182
00:13:38,240 --> 00:13:39,230
output.

183
00:13:39,230 --> 00:13:44,450
So now that standard error is going to standard output and all the standard output goes through the

184
00:13:44,450 --> 00:13:49,520
pipe then cat should count all of our lines let's hit enter and see if that happens.

185
00:13:49,580 --> 00:13:52,780
Sure enough it counts all six of our lines.

186
00:13:52,820 --> 00:13:54,520
Now there's a shorthand for this.

187
00:13:54,530 --> 00:13:56,660
It's pipe ampersand.

188
00:13:56,660 --> 00:14:03,800
So instead of supplying all this food supplied pipe Ampersand it combines the standard error and standard

189
00:14:03,800 --> 00:14:06,580
output into one and then passes that through the pipe.

190
00:14:06,800 --> 00:14:08,190
So we get the same result.

191
00:14:08,840 --> 00:14:09,170
OK.

192
00:14:09,170 --> 00:14:11,380
We've covered a lot here on the command line.

193
00:14:11,390 --> 00:14:17,570
Let's get back to our script and really do a mini recap of what we've been talking about and let's documented

194
00:14:17,600 --> 00:14:21,340
in our script so we have something to look back to if we want to later.

195
00:14:21,470 --> 00:14:28,030
So I'm going to edit our file and we're just going to be really repeating some of the examples above.

196
00:14:28,250 --> 00:14:30,140
But this time using file descriptors

197
00:14:35,280 --> 00:14:40,170
so we'll redirect standard into to a program using file descriptor 0

198
00:14:46,040 --> 00:14:47,390
we'll just display this back

199
00:14:51,870 --> 00:15:00,750
and now what we'll do is redirect standard out to a file using file descriptor one over writing that

200
00:15:00,750 --> 00:15:01,490
file.

201
00:15:02,550 --> 00:15:10,680
So let's do head dash in three let's see C password will be explicit here in use one and then give the

202
00:15:10,680 --> 00:15:11,640
path to the file

203
00:15:22,330 --> 00:15:29,980
let's just stop right here and say make changes and execute our script and see what happens so far more

204
00:15:29,980 --> 00:15:32,990
or less we get a repeat of what happens at the top of the script.

205
00:15:33,040 --> 00:15:36,000
We get line containing the first line of data.

206
00:15:36,100 --> 00:15:39,580
Now we get the contents of temp data containing three lines as well.

207
00:15:39,670 --> 00:15:45,610
Even though we use the explicit form of redirection by supplying file descriptors.

208
00:15:45,610 --> 00:15:47,700
So again it works the exact same way.

209
00:15:47,710 --> 00:15:49,890
Implicit or explicit.

210
00:15:49,930 --> 00:15:52,360
Let's go ahead and continue editing our script here

211
00:15:57,150 --> 00:16:06,180
by the way a standard error is abbreviated STV E R and standard error is a file descriptor too.

212
00:16:06,450 --> 00:16:09,600
So we're going to use this error file a couple of times.

213
00:16:09,750 --> 00:16:15,640
Let's create a new variable as well as a new fall for this error file r e r r file.

214
00:16:16,230 --> 00:16:18,690
Let's give this temp data dot e r r

215
00:16:24,140 --> 00:16:28,850
will give this file something that's going to generate an era file that doesn't exist.

216
00:16:28,850 --> 00:16:32,690
We'll redirect that error message into the error file.

217
00:16:34,010 --> 00:16:34,640
OK.

218
00:16:34,670 --> 00:16:42,660
Let's save our changes execute our script and we get the standard output to our screen.

219
00:16:42,980 --> 00:16:47,920
Let's see what the contents of this temp data e r file are.

220
00:16:48,020 --> 00:16:53,630
Sure enough that's the error message that was generated by the head command that we redirected into

221
00:16:53,630 --> 00:16:57,520
that file so that's doing exactly what we specified.

222
00:16:57,530 --> 00:17:00,140
Let's get back into our script and keep on going here.

223
00:17:00,140 --> 00:17:07,810
Now let's redirect the standard out and the standard error to a file we'll use our same head command

224
00:17:07,820 --> 00:17:10,110
here.

225
00:17:10,400 --> 00:17:15,860
Only this time we're going to use the new syntax the ampersand greater than sign and send that to a

226
00:17:15,860 --> 00:17:19,900
file and now we'll just display the contents here.

227
00:17:26,710 --> 00:17:30,360
Can't see where changes and execute our script.

228
00:17:30,520 --> 00:17:34,090
And sure enough everything is displayed to our screen there.

229
00:17:34,090 --> 00:17:41,170
So the standard output as well a standard error were both combined.

230
00:17:41,170 --> 00:17:50,790
Now let's redirect standard output and standard error through a pipe.

231
00:17:51,310 --> 00:17:54,900
We'll print a blank line and then we'll use our head command here

232
00:17:57,910 --> 00:18:04,060
we'll use the new syntax if you will the latest syntax of pipe Ampersand and we'll pipe that to the

233
00:18:04,060 --> 00:18:05,980
cat command with the dash in.

234
00:18:06,010 --> 00:18:09,970
That's going to number the standard input that cat receives.

235
00:18:09,970 --> 00:18:13,010
So we'll see where changes and execute our script.

236
00:18:13,090 --> 00:18:19,570
And sure enough the error message of cannot open fake file gets passed through the pipe to cat and gets

237
00:18:19,570 --> 00:18:20,860
numbered.

238
00:18:20,860 --> 00:18:24,460
So we've appended standard error to standard output.

239
00:18:24,520 --> 00:18:29,230
Let's do the opposite which is to append standard output to standard error.

240
00:18:29,650 --> 00:18:37,090
So if we take something like the echo command and just echo the word error pipe that into cat with a

241
00:18:37,090 --> 00:18:43,690
dash in option it just shows one line because one line came through a standard input into cat.

242
00:18:44,200 --> 00:18:46,000
So now let's do the same thing here.

243
00:18:46,000 --> 00:18:46,880
Echo Error.

244
00:18:47,440 --> 00:18:50,890
But this time let's use the redirect symbol.

245
00:18:51,160 --> 00:18:58,390
The ampersand which specifies a file descriptor and we'll use file descriptor too and pipe that to cat.

246
00:18:58,420 --> 00:19:03,730
Now remember that the ampersand that follows the redirection operator is used when you want to use a

247
00:19:03,730 --> 00:19:06,670
file descriptor instead of a file name.

248
00:19:06,670 --> 00:19:14,170
And to be super clear this is exactly the same as this one greater than Ampersand 2.

249
00:19:14,200 --> 00:19:20,140
Now the reason cat didn't put a line number in front of error is because the output of error never got

250
00:19:20,140 --> 00:19:27,080
to cat because it was redirected to standard error so echo Error generated standard error.

251
00:19:27,340 --> 00:19:30,750
So we forced echo to really generate standard error.

252
00:19:30,760 --> 00:19:35,980
Now why would you ever want to do this mainly so that you can make your scripts conform to the standard

253
00:19:35,980 --> 00:19:39,560
convention of sending error messages to standard error.

254
00:19:39,610 --> 00:19:45,370
If you find yourself writing an exit command in a script and echoing some information just before that.

255
00:19:45,370 --> 00:19:51,710
That's a really good sign that you need to send the output of those echo commands to standard error.

256
00:19:52,600 --> 00:19:57,640
So let's put a demonstration of how to send output to standard error.

257
00:19:57,640 --> 00:20:01,700
We'll be using this in a future script of our so we'll do echo.

258
00:20:01,750 --> 00:20:14,140
This is standard error and then do this greater than Ampersand to write or changes and execute our script

259
00:20:16,860 --> 00:20:24,060
now let's execute our script and redirect standard error to a file that we'll call ya.

260
00:20:24,060 --> 00:20:32,940
So now if we can't e are the standard error generated by our script is in that file before we wrap up

261
00:20:32,940 --> 00:20:38,790
this lesson I want to cover the null device that no device has a special file that throws away whatever

262
00:20:38,790 --> 00:20:39,810
is sent to it.

263
00:20:39,840 --> 00:20:44,700
Some people call this the bit bucket so if you don't want to see output on your screen and you don't

264
00:20:44,700 --> 00:20:50,250
want to save that output to a file either then redirect that output to the null device which is located

265
00:20:50,250 --> 00:20:53,430
at Forward slash Dev forward slash no.

266
00:20:53,430 --> 00:20:54,580
So here's an example.

267
00:20:54,600 --> 00:21:01,050
Had dash in one that's a password as he hosts fake file we'll just run this command and remind ourselves

268
00:21:01,050 --> 00:21:02,190
what this does.

269
00:21:02,280 --> 00:21:09,290
Now let's discard the standard output so we'll just send the standard output to Dev no.

270
00:21:09,330 --> 00:21:11,990
So also we're left with this standard error.

271
00:21:12,000 --> 00:21:17,310
Now we can do the same thing here and just throw away the standard error by doing to greater then and

272
00:21:17,310 --> 00:21:19,050
then the path to the node device.

273
00:21:19,050 --> 00:21:19,370
OK.

274
00:21:19,380 --> 00:21:22,010
Now we don't see any error messages.

275
00:21:22,080 --> 00:21:27,240
Now let's throw away all the output generated by the command and we know how to do that because we can

276
00:21:27,240 --> 00:21:32,730
use this Ampersand greater than sign to combined standard output and standard error and we'll redirect

277
00:21:32,730 --> 00:21:33,480
that to death.

278
00:21:33,480 --> 00:21:33,910
No.

279
00:21:34,440 --> 00:21:37,440
So no output is generated.

280
00:21:37,440 --> 00:21:40,250
You might wonder when you would want to do such a thing well.

281
00:21:40,380 --> 00:21:45,990
Here is the use case if you're executing a command in your script and you don't want the user running

282
00:21:45,990 --> 00:21:48,650
the script to see that output then send it to DAB.

283
00:21:48,660 --> 00:21:49,310
No.

284
00:21:49,410 --> 00:21:54,600
And remember if you need to know that a command succeeded or not simply check its exit status.

285
00:21:54,600 --> 00:21:57,970
You don't need the output to determine if it succeeded or failed.

286
00:21:58,050 --> 00:22:04,960
So we can do this echo dollar sign question mark and we get an exit status of one which is non-zero

287
00:22:04,980 --> 00:22:11,070
so something went wrong but if we make the command succeed for example take away that file that doesn't

288
00:22:11,070 --> 00:22:13,920
exist and then we check the return status.

289
00:22:13,980 --> 00:22:18,330
We get an exit status zero so we know that the command succeeded.

290
00:22:18,330 --> 00:22:22,650
Now let's finish up our little script here by adding these examples.

291
00:22:22,920 --> 00:22:25,800
Let's discard standard out

292
00:22:38,090 --> 00:22:41,820
and then let's discard standard error

293
00:22:54,340 --> 00:22:55,900
so we'll soon follow the too to death.

294
00:22:55,900 --> 00:22:56,260
No.

295
00:22:56,560 --> 00:23:04,080
And finally we'll just do an example here of discarding both a standard out and standard error

296
00:23:13,150 --> 00:23:15,870
we'll put a message here to try to show that we're doing that.

297
00:23:24,020 --> 00:23:26,710
And one thing I like to do is clean up after myself.

298
00:23:26,710 --> 00:23:32,170
I don't like to leave temporary files just laying around so I remember that we created two files that

299
00:23:32,170 --> 00:23:38,020
we were going to remove here will remove file and then we also created an error file that we can remove.

300
00:23:38,230 --> 00:23:42,850
And by the way this would be a good place to send to Dev.

301
00:23:42,880 --> 00:23:44,920
No.

302
00:23:44,920 --> 00:23:49,660
So if for some reason the arm command fails maybe we really don't care because it's a temporary file.

303
00:23:49,660 --> 00:23:51,820
We can just throw that out put away if we want to.

304
00:23:52,180 --> 00:23:52,940
So let's do that.

305
00:23:52,960 --> 00:23:54,870
As an example in this case.

306
00:23:55,060 --> 00:23:55,400
OK.

307
00:23:55,450 --> 00:24:01,170
We're almost done here we'll just exit on this file and then execute it and see what happens.

308
00:24:01,180 --> 00:24:06,160
So here's the example where we discarded standard output and we're only left with the error message.

309
00:24:06,220 --> 00:24:10,870
We did the reverse by discarding standard error so that only left the standard output.

310
00:24:10,870 --> 00:24:14,070
And then finally we discarded both standard out and standard error.

311
00:24:14,140 --> 00:24:20,830
And of course nothing was displayed to the screen in this lesson you learned a lot about input and output

312
00:24:20,860 --> 00:24:22,090
redirection.

313
00:24:22,090 --> 00:24:26,230
You learn that each new process starts with three open file descriptors.

314
00:24:26,350 --> 00:24:32,510
They are file descriptor 0 for standard input file descriptor 1 for standard output and file descriptor

315
00:24:32,530 --> 00:24:34,470
2 for standard error.

316
00:24:34,540 --> 00:24:40,000
You learn that the greater than signed redirects standard output to a file and that it creates the file

317
00:24:40,000 --> 00:24:45,940
if it doesn't exist or if that file does exist it truncate that file overwriting it.

318
00:24:45,940 --> 00:24:50,520
You learn that you have to use double greater than signs to append to a file without overwriting it.

319
00:24:51,430 --> 00:24:56,920
If no file descriptor is specified for output redirection file descriptor one is assumed that's why

320
00:24:56,920 --> 00:25:01,230
you had to specify the file descriptor to to redirect standard error.

321
00:25:01,300 --> 00:25:06,970
You learn that you can redirect both standard input and standard output to a file by using the ampersand

322
00:25:06,970 --> 00:25:09,040
greater than sine syntax.

323
00:25:09,040 --> 00:25:14,620
If you want to redirect both standard input and standard output through a pipe use the pipe Ampersand

324
00:25:14,620 --> 00:25:16,280
syntax.

325
00:25:16,360 --> 00:25:21,820
From there you moved on to redirecting standard input from a file into a command by using the less than

326
00:25:21,820 --> 00:25:22,810
sine.

327
00:25:22,900 --> 00:25:26,340
Finally you learned how to use the null device by redirecting output to Dev.

328
00:25:26,350 --> 00:25:27,220
No.

329
00:25:27,610 --> 00:25:30,760
I know we covered a lot in this lesson.

330
00:25:30,820 --> 00:25:36,430
If this is your first time being exposed to input and output redirection I would actually encourage

331
00:25:36,430 --> 00:25:38,410
you to watch this video again.

332
00:25:38,500 --> 00:25:42,640
Let it soak in for now and maybe come back to it tomorrow or in a day or two.
