Hey everybody welcome depart for the Windows command line mini module.
So in this video we're going to be talking about pipes the pipes that we're talking about in this video
are also called redirection.
They are not the same as the naming pipes that we were talking about before.
The reason pipe tends to get its name is because of that symbol right there which is just above the
enter key on the keyboard.
Now in Windows and Linux This is what we're going to be talking about mainly how in windows you have
this and this for redirection So what's the first thing.
We've already actually seen pipes before and redirection is already in this course.
So when we did when we echo the word we'll say read into data that takes time.
Right.
That son it is pushing in the debt the tech the output of the Echo command which is just printing the
word read into a file called data.
So if we come down here we can see reds here now if we change this to blue and did that you notice it
doesn't create a new file it just over writes the current one.
So that's important to note when you use the that particular symbol you are going to overwrite the file.
So go ahead and change this back to blue red.
So now what happens if we wanted to add blue to it.
So we can have an extra one which basically tells the system to append this to the end of a file.
So if we go and run that we again see data was not removed and now we see red and blue so it's good
to know that you can pop things to the end of a file or do a complete rewrite of it.
So again we'll do.
Let's see here
nation.
All right pop this in here.
There we go.
So the next step is what happens if we want it to go the other way.
So maybe we have a command called sort that would just sort each line alphabetically.
How do we get input.
Now we could just do data that takes t and this will sort it alphabetically which is which is fine.
So if we do the the actual file we see red starts first which is exactly what we put.
Quick thing.
So you guys we haven't gone over before but to clear the screen to kind of give yourself more room you're
going to do see less very clear screen now.
So we've got the sort.
Right.
We sort data but let's say we want to provided a different type of.
We want to provide sort something else from a file.
Right.
So we want to type the output directly to it.
Now what we can do is the opposite and pipe that into our pipe data into sort.
Now what this is doing behind the scenes is it's doing the work type data the data that takes D and
then pushing it into sort.
So as we can see it now sorts it correctly.
So that's piping a content from a file into an argument of a command.
Now that can be used in a lot of other different ways this is just one example.
Now the next part is is we want to be able to pipe one command the output of one command to another.
Now type data that t t is a command.
So it's grabbing the output so the output of that command is the actual data from text.
Now this could be another application or not we're just using type in this example.
What we're gonna do if we want to if we want to send information from one command to another or the
output of one command to a different command you're gonna use the pipe symbol.
Sorry about that
so when we use the pipe symbol then we can send this over to sort which will again give us our same
information.
Now you can use this to pipe out information from a custom script that you wrote and then sort out the
data.
This is where manipulation manipulation of data in files and binding strings and stuff like that's gonna
be very useful piping the output of one command to another into another into another so you can technically
sort this five or ten different ways so the next thing is we're going to get into a little bit more
of like a conditional or logic based.
So we want to do is what happens if we want to run one command after another.
Now that's pretty simple we can do type data up to 16 with the end sign that tells us that we won't
run something after the first one completes not at the same time but after it completes and then we're
going to go test to test up to.
So now we can see after that runs we have our test that takes t down here.
Perfect gluttony give ourselves some room.
Now what happens if we want to run the first command but only if it succeeds and runs correctly.
Do we want to run something else.
So in that particular case we're going to do the IRS Slash Y so as you can see this fails.
So the failure of a command is when it throws an error of some kind or it ends up with a return code
of something that's not zero.
So running DPR like this will return an error code or a result code of zero and doing something like
this will return result code 1.
So that's how it checks whether it's going to fail or not.
So we send the command we know it's going to fail two ends then we're going to go test into test up
to 60.
Let's go out and get rid of this so if this command fails which it will we should get a text that text
file on the bottom left
I'm sorry that the command succeeds.
We'll see it.
So if we see that a command failed it did not.
So if we change this to succeed now we should see the file.
There we go.
Perfect.
So I'm glad that now we can also do the reverse.
So if it fails we want to run this command and we can do that by changing the answer to pipes.
So if it fails it will run the next command afterwards.
Perfect.
And if it does not fail
delete this file if it does not fail then it should not create the file and it does not perfect.
Now we can get a bit more detailed so let's say we want to run a specific command and if it works we
want to do run run one command and if it fails we want to run another right.
So we'll see.
Same thing with the IRR.
So if we want this to fail.
So we're going to make it fail at this point.
We're going to end and then we're going to echo the word worked
worked into worked up to 60 and we're going to do the double ores so the double pipes then echo failed
to failed up to 60.
Now if the first command fails was in the D.R. Slash Y.
If this fails we're gonna end up running this echo failed command and we're going to skip the work one
if it works correctly then we will see the worked command.
But in this case we did the Slash Y so we know we'll fail.
So we should get a failed one down here and we do.
So if we swap this around and remove the failed command now we should get the worked perfect.
Now understanding how piping works and doing some of these logical things on the command line can allow
you to do more complex things in a in a more compact way.
So being able to run commands one after another while having some produce input some passing input over
maybe only one or two pass input so you can get really complex with this.
So go ahead and play around with this and see what you guys come up with.
There's much more details that you can look up online around how to do different set of stuff like this.
So essentially what we're doing is we're doing bash when it batch when it comes to this.
So these are all things you could do the batch the batch scripting language which feel free to look
up on we're not going to necessarily go through that in this video but understanding how these commands
can be tied together is very important.