In this lesson you're going to learn about the different types of input and output and how to redirect
and control those types of input and output.
So as always I have a local terminal up on my machine and I'm going to move into our working folder
for this class and into the project folder.
Next I'm going to bring up our virtual machine
now that the virtual machine is up I'm going to connect to it from here I'm going to move into the shared
folder of forward slash vagrant.
The name I'm going to use for our demo script today is user dash demo 0 8.
SH I'm going to add the shebang here and give a path to the bash executable and I'll just add a short
header to our file
so far on this course.
The only type of input and output redirection that you used has been in the form of a pipe you've already
written scripts that redirect the standard output from one command and send it as standard input into
another.
By separating those commands with the pipe you've used that method to string together several commands
in order to generate a random password for example you also used to send that random password to the
password command.
Let's take a step back and talk about the different types of input and output.
First off there are three types of IO.
They are standard input standard output and standard error by default standard input comes from the
keyboard.
For example the read command accept standard input and you use the read command to collect information
from a user typing something in at their keyboard.
You also know however that standard input doesn't have to come from a keyboard.
Again you use the pipes to take the output generated by one command and use that as the standard input
for another command.
Beyond standard input there is standard output and standard error.
But if both standard output and standard error are displayed to the screen.
Let's start out by redirecting the standard output of a command into a file and you can do that by using
the greater than sign.
By the way I often see Standard out abbreviated as STV out we're going to be using a file several times
throughout the script so I'm just going to assign that file name to a variable.
I'm going to call that variable file and then I'm going to say that that file is going to live and forward
slash temp and we'll call the name of that file data.
Let's use a command here that we've already used in a previous exercise.
We'll use the head command dash and one will output the very first line of a file and we'll just look
at the first line of the ETSI password file.
And also the output of this command the standard output of this command and to the file.
Now remember by default standard output is displayed to the screen.
But since we're using the greater than symbol after the command it is redirecting that output away from
the screen and into the file we specified after the greater than sign.
So let's see what happens when we actually execute this script.
So I'm going to save my changes.
Make sure that this is executable
and then execute the script.
As you can see no output was generated by our script and that's pretty much to be expected because we
told the output of the head command that's in the script to go to a file.
Now let's look at that file.
Ok that seems to be the output of head dash in one ETSI password and we can actually confirm this by
just running the command here on the command line and sure enough that is the output of the command
that's in our script and that output was not displayed to the screen but instead went to the file where
we redirected that output to be super clear this output redirection is not part of any one command.
It works with all commands.
While we're on the command line let's try it out with let's say the ID command.
So we'll use ID dash you when you use this in a previous project and we'll send the standard output
to a file that will decide to call ID.
Again this file name could be anything.
So no output goes to the screen.
However if we look at the contents of the ID file with the cat command we see the output that would
be generated by that command.
Again we can do this with any command.
Let's use echo
if we can to you I.D. file Sure enough the output of the equil command is in the file that we redirected
that output to.
By the way file permissions are in play here too.
So if you try to redirect output into a file where you don't have write permissions you're going to
get an error.
So let's back up here with our echo command and let's try to write this and say forward slash you ID
and hit enter.
And sure enough we get a permission denied error.
Let's look at the permissions on forward slash.
There's set 2 5 5 5 or read execute read execute execute with the owner of route and the group of Route
Well we're vagrant and we're in the group of vagrants So we have no right permissions so we can't create
files there.
We can't do that either through redirection or using an editor or any other means we just simply don't
have permissions.
I just wanted to point out that if you get a permission denied error when you're working with redirection
take it at face value.
You don't have permissions there.
Kate let's jump back into our script.
Ok now we know that the greater than sign redirects standard output while to redirect standard input
use the less than sign
against standard input is often abbreviated as TV.
I meant let's use an example with the read command will do read line do an input redirection and then
supply a file and we'll just use the same file that we created with the head command.
Now you use the read command to accept standard input from a user by using imper redirection you are
now accepting standard input from a file.
The command reads one line of standard input.
When someone is typing at the keyboard and they hit enter that represents one line of standard input.
Of course when dealing with files a newline character will cause read to stop.
So the variable line should contain the first line of the file.
In this example there is only one line in the file.
Let's prove that by echoing it to the screen will say that the line variable contains the contents of
the line variable here and hit enter.
Let's save our changes and execute the script.
Stepping through this short script we used output redirection to send the first line of the ETSI password
file into a file named Ford slash temp forge slash data.
That file was then used as standard input to the read command which assigned the value to the line variable.
Finally the line variable was displayed to standard output using the echo command.
And that gives you what you see on your screen here redirecting standard input from a file works with
any command that accepts standard input.
Take the password command as an example.
Let's put some text into a file and we can do that with the echo command will just echo out some information
here.
Normally that would be displayed to our screen but we're going to use output redirection to put that
output in a file called P-A s w o r d.
So we'll look at the contents of that file sure enough that contains the word secret.
Now that use that file a standard input to the password command.
So we need root privileges to do this we're going to use Sea-Doo password with a standard option and
we need to supply a user while I have the Einstein user set up on this system.
So I'm going to use that user and then I'm going to redirect in the standard input that lives in the
file password.
So we're going to redirect the contents of the file name password as standard input into the P.A. accessed
at the command.
It says the passwords were changed for the user Einstein and I can verify this by just switching users
and I'm going 200 s e c r t and hit enter.
And sure enough it accepts the password so that works.
So the first method you used was with a pipe but now you can also use this input redirection with the
less than sign and remember the difference between those methods is that in a pipe the standard input
comes from the output of a command.
Whereas with this less than symbol the standard input comes from a file.
So that's the difference.
You use a pipe when you want to take output from a command as input into another command but you can
use this redirection when you want to redirect the contents of a file into a command.
It's important to know that using a single grader inside to perform a redirection creates a new file.
If that file doesn't exist or it overwrites the contents of that file if it does exist.
So right now the password file contains the word secret.
If we perform a redirection to that file it will get overridden.
So we'll do this will just echo the word new and to the password file.
So now when we look at the contents of that file it contains new notes.
Demonstrate this in our script.
So now we're going to take the first three lines of the ETSI password file and send that into this file.
Going to echo a blank line to the screen and then now show the contents of that file.
Hey let's save our changes and execute the script.
The first time we performed a redirection to the script we took the first line at the ETSI password
file and put it into a while.
The next time we perform a redirection to that file we took the first three lines of the password file
and wrote it to that file.
So here you can see that the contents of temp data contained three lines not four lines it didn't append
to it.
It Over wrote the contents of that file.
So what if you don't want to overwrite a file but you want to add or append to it.
In that case you use double.
Greater than symbols.
So let's look at the contents of our password file here in our current directory.
It contains just one line and that line says new.
Now let's add another line to the file.
We'll do this echo another line and then we'll use two greater than symbols.
And by the way they can have a space between them they should be together.
And we're going to provide the name of that file that we're going to redirect this output into the name
of that Folles ph SS w o r d.
So now when we look at the contents of this file we should see two lines.
And sure enough we do the original contents which was new and then the new contents which is another
dash line.
Let's keep adding to this file by using one of our password generation techniques.
We'll just take the current date take the output of that as input to the Shah 256 some command and then
we'll just take the first 10 characters that that command generates and then we'll appended to the P
S S W O R D file.
Now the file contains that extra data.
Now I think it's pretty darn awesome how you can combine different types of input and output redirection
all on the same command line.
So here we use pipes and output redirection.
Let's set this as an example of IO redirection to our script.
So now we're going to redirect the standard out to a file appending to that file.
So let's just echo a couple of random numbers in there.
And then we'll just repeat this line.
So I missed a dollar sign in front of this variable and went to place it there.
Now I'll just add a blank line and then echo the contents of that file.
Let's save our changes and execute the script.
OK so originally the contents of temp data was one line.
It was overwritten by three lines.
And then we performed and append the output redirection.
And so now that has 5 lines you can see the random numbers that we generated and appended to the file.
So we've covered redirecting standard input and standard output.