1
00:00:03,660 --> 00:00:09,550
In this lesson you're going to learn about the different types of input and output and how to redirect

2
00:00:09,550 --> 00:00:12,160
and control those types of input and output.

3
00:00:12,460 --> 00:00:17,710
So as always I have a local terminal up on my machine and I'm going to move into our working folder

4
00:00:17,710 --> 00:00:23,000
for this class and into the project folder.

5
00:00:23,110 --> 00:00:25,210
Next I'm going to bring up our virtual machine

6
00:00:29,520 --> 00:00:35,980
now that the virtual machine is up I'm going to connect to it from here I'm going to move into the shared

7
00:00:35,980 --> 00:00:38,540
folder of forward slash vagrant.

8
00:00:38,800 --> 00:00:45,270
The name I'm going to use for our demo script today is user dash demo 0 8.

9
00:00:45,370 --> 00:00:53,200
SH I'm going to add the shebang here and give a path to the bash executable and I'll just add a short

10
00:00:53,350 --> 00:00:54,480
header to our file

11
00:01:01,720 --> 00:01:02,740
so far on this course.

12
00:01:02,740 --> 00:01:09,340
The only type of input and output redirection that you used has been in the form of a pipe you've already

13
00:01:09,340 --> 00:01:15,420
written scripts that redirect the standard output from one command and send it as standard input into

14
00:01:15,420 --> 00:01:16,080
another.

15
00:01:16,090 --> 00:01:22,020
By separating those commands with the pipe you've used that method to string together several commands

16
00:01:22,030 --> 00:01:28,630
in order to generate a random password for example you also used to send that random password to the

17
00:01:28,630 --> 00:01:30,340
password command.

18
00:01:30,340 --> 00:01:35,470
Let's take a step back and talk about the different types of input and output.

19
00:01:35,470 --> 00:01:38,120
First off there are three types of IO.

20
00:01:38,140 --> 00:01:44,830
They are standard input standard output and standard error by default standard input comes from the

21
00:01:44,830 --> 00:01:45,540
keyboard.

22
00:01:45,670 --> 00:01:51,190
For example the read command accept standard input and you use the read command to collect information

23
00:01:51,190 --> 00:01:54,150
from a user typing something in at their keyboard.

24
00:01:54,340 --> 00:01:58,600
You also know however that standard input doesn't have to come from a keyboard.

25
00:01:58,630 --> 00:02:04,840
Again you use the pipes to take the output generated by one command and use that as the standard input

26
00:02:04,840 --> 00:02:06,450
for another command.

27
00:02:06,710 --> 00:02:10,790
Beyond standard input there is standard output and standard error.

28
00:02:10,960 --> 00:02:15,510
But if both standard output and standard error are displayed to the screen.

29
00:02:15,820 --> 00:02:21,490
Let's start out by redirecting the standard output of a command into a file and you can do that by using

30
00:02:21,490 --> 00:02:27,130
the greater than sign.

31
00:02:27,410 --> 00:02:34,310
By the way I often see Standard out abbreviated as STV out we're going to be using a file several times

32
00:02:34,310 --> 00:02:38,660
throughout the script so I'm just going to assign that file name to a variable.

33
00:02:38,720 --> 00:02:44,400
I'm going to call that variable file and then I'm going to say that that file is going to live and forward

34
00:02:44,420 --> 00:02:48,630
slash temp and we'll call the name of that file data.

35
00:02:49,040 --> 00:02:53,250
Let's use a command here that we've already used in a previous exercise.

36
00:02:53,270 --> 00:03:00,470
We'll use the head command dash and one will output the very first line of a file and we'll just look

37
00:03:00,470 --> 00:03:03,210
at the first line of the ETSI password file.

38
00:03:03,410 --> 00:03:09,500
And also the output of this command the standard output of this command and to the file.

39
00:03:09,500 --> 00:03:13,500
Now remember by default standard output is displayed to the screen.

40
00:03:13,550 --> 00:03:19,370
But since we're using the greater than symbol after the command it is redirecting that output away from

41
00:03:19,370 --> 00:03:23,700
the screen and into the file we specified after the greater than sign.

42
00:03:23,840 --> 00:03:27,040
So let's see what happens when we actually execute this script.

43
00:03:27,260 --> 00:03:29,270
So I'm going to save my changes.

44
00:03:29,360 --> 00:03:31,130
Make sure that this is executable

45
00:03:34,510 --> 00:03:36,510
and then execute the script.

46
00:03:38,020 --> 00:03:43,630
As you can see no output was generated by our script and that's pretty much to be expected because we

47
00:03:43,630 --> 00:03:48,010
told the output of the head command that's in the script to go to a file.

48
00:03:48,010 --> 00:03:50,310
Now let's look at that file.

49
00:03:52,500 --> 00:03:58,080
Ok that seems to be the output of head dash in one ETSI password and we can actually confirm this by

50
00:03:58,080 --> 00:04:04,910
just running the command here on the command line and sure enough that is the output of the command

51
00:04:04,910 --> 00:04:10,460
that's in our script and that output was not displayed to the screen but instead went to the file where

52
00:04:10,460 --> 00:04:18,400
we redirected that output to be super clear this output redirection is not part of any one command.

53
00:04:18,410 --> 00:04:19,920
It works with all commands.

54
00:04:20,030 --> 00:04:24,110
While we're on the command line let's try it out with let's say the ID command.

55
00:04:24,290 --> 00:04:29,900
So we'll use ID dash you when you use this in a previous project and we'll send the standard output

56
00:04:29,930 --> 00:04:32,760
to a file that will decide to call ID.

57
00:04:32,820 --> 00:04:34,600
Again this file name could be anything.

58
00:04:34,790 --> 00:04:36,440
So no output goes to the screen.

59
00:04:36,440 --> 00:04:41,570
However if we look at the contents of the ID file with the cat command we see the output that would

60
00:04:41,570 --> 00:04:43,410
be generated by that command.

61
00:04:43,790 --> 00:04:45,500
Again we can do this with any command.

62
00:04:45,500 --> 00:04:46,640
Let's use echo

63
00:04:51,230 --> 00:04:57,110
if we can to you I.D. file Sure enough the output of the equil command is in the file that we redirected

64
00:04:57,110 --> 00:04:58,580
that output to.

65
00:04:58,610 --> 00:05:01,510
By the way file permissions are in play here too.

66
00:05:01,520 --> 00:05:06,380
So if you try to redirect output into a file where you don't have write permissions you're going to

67
00:05:06,380 --> 00:05:07,580
get an error.

68
00:05:07,580 --> 00:05:14,680
So let's back up here with our echo command and let's try to write this and say forward slash you ID

69
00:05:14,690 --> 00:05:15,650
and hit enter.

70
00:05:15,800 --> 00:05:17,820
And sure enough we get a permission denied error.

71
00:05:18,080 --> 00:05:20,160
Let's look at the permissions on forward slash.

72
00:05:20,270 --> 00:05:26,240
There's set 2 5 5 5 or read execute read execute execute with the owner of route and the group of Route

73
00:05:26,450 --> 00:05:31,970
Well we're vagrant and we're in the group of vagrants So we have no right permissions so we can't create

74
00:05:31,970 --> 00:05:32,790
files there.

75
00:05:32,870 --> 00:05:38,270
We can't do that either through redirection or using an editor or any other means we just simply don't

76
00:05:38,270 --> 00:05:39,130
have permissions.

77
00:05:39,140 --> 00:05:44,240
I just wanted to point out that if you get a permission denied error when you're working with redirection

78
00:05:44,330 --> 00:05:45,790
take it at face value.

79
00:05:45,830 --> 00:05:47,670
You don't have permissions there.

80
00:05:48,080 --> 00:05:50,920
Kate let's jump back into our script.

81
00:05:51,710 --> 00:05:57,020
Ok now we know that the greater than sign redirects standard output while to redirect standard input

82
00:05:57,110 --> 00:05:58,670
use the less than sign

83
00:06:06,130 --> 00:06:09,370
against standard input is often abbreviated as TV.

84
00:06:09,450 --> 00:06:19,020
I meant let's use an example with the read command will do read line do an input redirection and then

85
00:06:19,020 --> 00:06:25,300
supply a file and we'll just use the same file that we created with the head command.

86
00:06:25,920 --> 00:06:31,530
Now you use the read command to accept standard input from a user by using imper redirection you are

87
00:06:31,530 --> 00:06:34,510
now accepting standard input from a file.

88
00:06:34,680 --> 00:06:38,250
The command reads one line of standard input.

89
00:06:38,250 --> 00:06:43,800
When someone is typing at the keyboard and they hit enter that represents one line of standard input.

90
00:06:43,950 --> 00:06:48,420
Of course when dealing with files a newline character will cause read to stop.

91
00:06:48,420 --> 00:06:52,880
So the variable line should contain the first line of the file.

92
00:06:52,890 --> 00:06:55,680
In this example there is only one line in the file.

93
00:06:55,710 --> 00:07:03,360
Let's prove that by echoing it to the screen will say that the line variable contains the contents of

94
00:07:03,360 --> 00:07:06,560
the line variable here and hit enter.

95
00:07:06,570 --> 00:07:09,150
Let's save our changes and execute the script.

96
00:07:11,340 --> 00:07:16,860
Stepping through this short script we used output redirection to send the first line of the ETSI password

97
00:07:16,860 --> 00:07:21,700
file into a file named Ford slash temp forge slash data.

98
00:07:21,840 --> 00:07:28,560
That file was then used as standard input to the read command which assigned the value to the line variable.

99
00:07:28,560 --> 00:07:33,470
Finally the line variable was displayed to standard output using the echo command.

100
00:07:33,630 --> 00:07:39,210
And that gives you what you see on your screen here redirecting standard input from a file works with

101
00:07:39,210 --> 00:07:41,740
any command that accepts standard input.

102
00:07:41,760 --> 00:07:44,100
Take the password command as an example.

103
00:07:44,220 --> 00:07:49,680
Let's put some text into a file and we can do that with the echo command will just echo out some information

104
00:07:49,680 --> 00:07:50,340
here.

105
00:07:50,340 --> 00:07:54,840
Normally that would be displayed to our screen but we're going to use output redirection to put that

106
00:07:54,840 --> 00:07:59,440
output in a file called P-A s w o r d.

107
00:07:59,880 --> 00:08:04,250
So we'll look at the contents of that file sure enough that contains the word secret.

108
00:08:04,260 --> 00:08:07,940
Now that use that file a standard input to the password command.

109
00:08:08,130 --> 00:08:15,030
So we need root privileges to do this we're going to use Sea-Doo password with a standard option and

110
00:08:15,030 --> 00:08:19,520
we need to supply a user while I have the Einstein user set up on this system.

111
00:08:19,650 --> 00:08:25,560
So I'm going to use that user and then I'm going to redirect in the standard input that lives in the

112
00:08:25,560 --> 00:08:26,770
file password.

113
00:08:27,000 --> 00:08:33,140
So we're going to redirect the contents of the file name password as standard input into the P.A. accessed

114
00:08:33,150 --> 00:08:34,890
at the command.

115
00:08:34,890 --> 00:08:41,250
It says the passwords were changed for the user Einstein and I can verify this by just switching users

116
00:08:42,630 --> 00:08:45,880
and I'm going 200 s e c r t and hit enter.

117
00:08:45,900 --> 00:08:49,320
And sure enough it accepts the password so that works.

118
00:08:49,410 --> 00:08:56,130
So the first method you used was with a pipe but now you can also use this input redirection with the

119
00:08:56,130 --> 00:09:02,640
less than sign and remember the difference between those methods is that in a pipe the standard input

120
00:09:02,670 --> 00:09:04,630
comes from the output of a command.

121
00:09:04,800 --> 00:09:08,630
Whereas with this less than symbol the standard input comes from a file.

122
00:09:08,640 --> 00:09:09,870
So that's the difference.

123
00:09:09,870 --> 00:09:15,360
You use a pipe when you want to take output from a command as input into another command but you can

124
00:09:15,360 --> 00:09:19,980
use this redirection when you want to redirect the contents of a file into a command.

125
00:09:20,250 --> 00:09:25,230
It's important to know that using a single grader inside to perform a redirection creates a new file.

126
00:09:25,230 --> 00:09:30,150
If that file doesn't exist or it overwrites the contents of that file if it does exist.

127
00:09:30,300 --> 00:09:34,560
So right now the password file contains the word secret.

128
00:09:34,560 --> 00:09:37,980
If we perform a redirection to that file it will get overridden.

129
00:09:38,040 --> 00:09:43,040
So we'll do this will just echo the word new and to the password file.

130
00:09:43,080 --> 00:09:47,370
So now when we look at the contents of that file it contains new notes.

131
00:09:47,370 --> 00:09:58,990
Demonstrate this in our script.

132
00:09:59,400 --> 00:10:06,340
So now we're going to take the first three lines of the ETSI password file and send that into this file.

133
00:10:06,630 --> 00:10:12,580
Going to echo a blank line to the screen and then now show the contents of that file.

134
00:10:20,490 --> 00:10:23,130
Hey let's save our changes and execute the script.

135
00:10:25,830 --> 00:10:30,990
The first time we performed a redirection to the script we took the first line at the ETSI password

136
00:10:30,990 --> 00:10:33,210
file and put it into a while.

137
00:10:33,390 --> 00:10:38,670
The next time we perform a redirection to that file we took the first three lines of the password file

138
00:10:38,850 --> 00:10:40,790
and wrote it to that file.

139
00:10:40,800 --> 00:10:46,770
So here you can see that the contents of temp data contained three lines not four lines it didn't append

140
00:10:46,770 --> 00:10:47,260
to it.

141
00:10:47,340 --> 00:10:49,830
It Over wrote the contents of that file.

142
00:10:49,980 --> 00:10:54,560
So what if you don't want to overwrite a file but you want to add or append to it.

143
00:10:54,660 --> 00:10:56,430
In that case you use double.

144
00:10:56,430 --> 00:10:57,790
Greater than symbols.

145
00:10:58,140 --> 00:11:02,500
So let's look at the contents of our password file here in our current directory.

146
00:11:02,730 --> 00:11:06,330
It contains just one line and that line says new.

147
00:11:06,330 --> 00:11:08,440
Now let's add another line to the file.

148
00:11:08,460 --> 00:11:13,890
We'll do this echo another line and then we'll use two greater than symbols.

149
00:11:13,920 --> 00:11:18,330
And by the way they can have a space between them they should be together.

150
00:11:18,720 --> 00:11:24,390
And we're going to provide the name of that file that we're going to redirect this output into the name

151
00:11:24,390 --> 00:11:27,770
of that Folles ph SS w o r d.

152
00:11:27,780 --> 00:11:31,860
So now when we look at the contents of this file we should see two lines.

153
00:11:32,730 --> 00:11:38,880
And sure enough we do the original contents which was new and then the new contents which is another

154
00:11:38,880 --> 00:11:39,840
dash line.

155
00:11:40,230 --> 00:11:44,600
Let's keep adding to this file by using one of our password generation techniques.

156
00:11:44,610 --> 00:11:52,380
We'll just take the current date take the output of that as input to the Shah 256 some command and then

157
00:11:52,380 --> 00:11:58,860
we'll just take the first 10 characters that that command generates and then we'll appended to the P

158
00:11:58,940 --> 00:12:02,910
S S W O R D file.

159
00:12:02,920 --> 00:12:05,970
Now the file contains that extra data.

160
00:12:05,980 --> 00:12:10,720
Now I think it's pretty darn awesome how you can combine different types of input and output redirection

161
00:12:10,990 --> 00:12:12,820
all on the same command line.

162
00:12:12,820 --> 00:12:16,510
So here we use pipes and output redirection.

163
00:12:16,510 --> 00:12:20,400
Let's set this as an example of IO redirection to our script.

164
00:12:23,200 --> 00:12:31,590
So now we're going to redirect the standard out to a file appending to that file.

165
00:12:31,630 --> 00:12:35,660
So let's just echo a couple of random numbers in there.

166
00:12:39,520 --> 00:12:42,180
And then we'll just repeat this line.

167
00:12:42,470 --> 00:12:46,500
So I missed a dollar sign in front of this variable and went to place it there.

168
00:12:46,680 --> 00:12:53,580
Now I'll just add a blank line and then echo the contents of that file.

169
00:12:57,570 --> 00:12:59,850
Let's save our changes and execute the script.

170
00:13:03,330 --> 00:13:07,190
OK so originally the contents of temp data was one line.

171
00:13:07,220 --> 00:13:09,650
It was overwritten by three lines.

172
00:13:09,860 --> 00:13:13,040
And then we performed and append the output redirection.

173
00:13:13,070 --> 00:13:19,930
And so now that has 5 lines you can see the random numbers that we generated and appended to the file.

174
00:13:19,940 --> 00:13:23,350
So we've covered redirecting standard input and standard output.
