Hello and welcome to Comment Line.
This video we're going to go over
piping in ampersand commands at the
command line, how we can use them to
our advantage to
begin learning
objectives. For this video, we're
going to learn what piping and used
for what the single and the double
pipes do and understand
what the single ampersand and the
double ampersand operands are used
for and how we can use them at the
command line
I preexistent, which often
runs command to if command
one fails, is it a
command one single pipe command
to be command one
double pipe command to
C command one single
ampersand command two
or D command one double
ampersand command to
answer is B command one
double pipe command to.
And as we'll learn in this
video, the Double Pi will
run a second command
if the first command fails.
This is very useful for trying to
keep track of if a certain
command is working properly
or not.
And similarly with the double
ampersand, it will run command to
its command. One is
executed properly, not if
it fails.
So we'll learn more about this in
this video.
It's the single pipe.
This is something that we've done a
lot already. In this course, it'll
take the standard output from the
first command and it will pass it as
standard input to this second
in command.
Very useful way to link two commands
together. And it gives us a lot
of really strong functionality
and opportunity at the command line.
And as we were using it, it's
used a lot with less or more.
And we can also use it with grip
so that we can look through and grep
for files and
execute these different commands
together by piping all of the output
to the later commands.
So very useful.
You should be very comfortable with
the signal because you're going to
use it a lot at the command line.
And it is a very, very effective
way to utilize the output from
commands in order to be able to
run additional commands and
manipulate that data.
So the double pipe, this is used
to execute a second command only
if the first one fails.
So this is really useful if you're
looking for functionality or if
we're using it in scripting where
if we're running a script and we're
going through a huge list of
computer names and let's say
that it cannot connect to a specific
computer game.
Well, if we use the double pipe, we
can say, hey, if this command
does not work for this computer
because that computer's offline or
it's not connected, shut down for
whatever reason it is, then I want
you to echo, hey, I
can't connect.
And as you can see in this first
example, if we do ping,
this house does not exist.
It will echo, hey, I can't ping it.
So let's give a quick
demonstration of that.
So we're going to do
ping. This does not
one not that
exist.
And this is not a real address.
So there is no way is going to ping.
It will do a double
echo.
I can't ping
now since we know that this house
does not exist, we can already know
that this ping command is going
to fail.
Since we're using the double pipe,
we know that it's going to execute
this echo command.
So check it out.
So Ping request cannot not find the
host. This does not exist.
And since that command failed,
it ran the echo command.
I can't ping and
now we can validate this where
we can ping our lookback
address.
And this is one twenty seven 0.01.
And if you're not familiar with the
loop back, it is a non ratable
only local system address.
So we know that every single time
is going to be able to ping this and
since it can ping it, it's going to
ignore this double pipe.
So don't run through really quick
and then we go and finish the ping
and we did not get the echo.
I can't ping because that
first command executed properly.
So we the ampersand, the single
ampersand on windows, it
is used to execute a additional
command after the first command.
This is not checked for any error
or any success for the first
command. And it is really
useful be able to submit a bunch of
different commands all on the same
line. So this is a useful
way to string a bunch of different
commands all together, all at once.
And if you're not familiar with
Linux, it's similar to the
semicolon on the Linux
Shell.
So we'll do a quick demonstration of
that or I will do echo
one, ampersand
echo two.
So it's going to do echo for one and
then immediately execute Echo two.
And as you can see, we have one end
or two. So executed those commands
directly.
And it does not check for an error
or success for
Linux, the single ampersand
is used to execute and run a
process in the background.
Now, when I'm using the Linux
terminal, I like to use the single
ampersand to start a
new application from
the command line and allows
me to keep control of the terminal
as well.
So that's a really useful
application of being able to use it.
You can do a quick demonstration of
that. So I have my Linux
Mint machine up.
Now let's say I want to
open up Firefox,
so I'm going to type in Firefox
and a single ampersand
and as you can see, is starting
on page thirty six.
Sixty two.
I still have control over my
terminal and Firefox
is taking its time starting up.
And here we go.
So we've got Firefox up
where if we just type Firefox
without the let me spell
it properly Firefox
without the ampersand, then
the terminal is going to hang and
it will only give us access back to
the terminal once
Firefox is closed.
So as you can see, we can type in
everything, but nothing is not
working.
So let's close it out.
And now it'll finally give us access
back to the terminal.
All right, with a double ampersand,
so what this does is it will execute
a second command only if the first
command was successful, so
it will only execute if there were
no errors on this first command.
So this is another useful way to be
able to use these
kind of operations in order to
get what we need.
So this is the opposite of the
double pipe where if we ping
our loop back address and say Echo
good, we know every single time
that is going to be a valid use.
And then if we ping something that
we know is not going to work, then
we know that we are not going
to get that echo
because it only works on
two on a successful
command.
So let's say ping
two seven zero zero one
double ampersand echo
works.
And since we know we can always ping
our loop back address, it's always
going to echo works
that second command.
Now, likewise, if we do
a host that we know does not exist,
this does not
exist.
And then we run the double ampersand
and echo works.
We know it will not work.
We had the error for pain because
it could not work properly and it
did not execute that second command
post assessment which operand will
execute command to if command
one is successful.
Is it a command one
single ampersand command to
be command one double ampersand
command to C
command one single ampersands
single command to
command one double command
to
the answer is B command one
double ampersand command to
this operation will only execute
command to if command one is
successful and similar to the double
play. This is a very useful
way to be able to control the flow
of data and help
identify commands
that should be running properly and
should not.
The single ampersand is used for
its own specific ways and Windows
and Linux not to help
with the command flow.
The single ampersand single pipe is
not a valid combination of these
characters, and the double pipe will
only execute command to if it
is unsuccessful for command one
or two. In this video we learned
what Peiping is used for, but single
and double pipes do not understood
the single ampersand in the double
ampersand operands and
how we can use them at the command
line to our advantage.
And I hope to see you in the next
video.