[Intro Music]
In Linux, it's a very common
practice to redirect data
streams to other processes, and send
some data to files for example, and
here on this diagram you see some
sample interaction between
different processes. In this
process you see STDOUT and STDERR;
and we redirect STDOUT
data stream to STDIN of other
process. This process has also of
course, STDOUT and STDERR data
streams. And in this example, those
data streams send data to specific
file; and you could use even same
file for both data streams. In such
case, STDOUT and STDERR will
write data to the same file. Next,
you see that this STDERR data
stream redirects data to STDIN
of another process; and this process
sends STDERR results to another
file. Also, you see that STDOUT of
this process does not send data to
other process or to other file. And
that means that if this process is
running inside of specific bash, or
sh shell, then you'll see result
of this STDOUT data stream inside
of the terminal where you are
running bash or sh process. Now,
let me jump into the terminal, and
explain you all of that on
practice. Let me clear the terminal
here. And I'll use first 'cat'
command. 'cat' command allows you to
read contents of a specific file or
read contents from STDIN, and that's
actually first utility that I'm
showing to you in this course, that
allows you to read data from STDIN;
I mean, you're able to send
data to STDIN of this process; and
this process will accept this data
and process it. Alright, let's go
back to terminal; and first let's
have a look at the help for this
command. Let me scroll up a bit,
and here you'll see that this
command actually concatenates file
or files and sends data to standard
output. With no file, because you see
that the file is actually optional
argument. This utility, 'cat'
utility will read data from
standard input. Let's try that.
Let's try this utility without any
argument, simply 'cat', press Enter,
and now I see that this process is
running. If I enter another tab and
let me quickly have a look at
processes running inside of this
container, 'docker exec -it', let
me copy this CONTAINER ID; paste
here; and I could simply type here
'ps'; and this will start be a process
inside of the running container;
let's press Enter; actually I need
to add here option 'ps -e', I
want to see all processes, and yes
now I see here 'cat' process that is
currently running; and here's
its Process ID. Great. Here while
this process is running we are able
to send some data to STDIN of this
process. Let's type "Hello cat", like
so, Enter, and I see result, and that
means that "Hello cat" was sent as
STDIN data stream to 'cat' process,
and it has sent it back via STDOUT
data stream, and that's why we have
seen this line in the terminal.
Notice that if I press simply Enter,
I get actually two empty lines, and
that means that this Enter is also
returned back by STDOUT data
stream of 'cat' process. Let's type
something else, "Hi", "Hey", "Good morning",
and so on. That's how you're able to
interact with STDIN data stream of
specific process. All right, let me
terminate this process, Ctrl+C. Now
it was terminated. And if I list
processes now, 'ps', I don't see 'cat'
process here on the list. It was
stopped. But that was example of the
process that has STDIN open; and
you could send data via this STDIN
data stream; and it sends same data
back via STDOUT data stream.
Alright, if I use the same command
but with argument, let's have a look
at the contents of this folder; and
here I still see two different
files 'stderr.txt' and 'stdout.txt'.
I could actually read
them using 'cat' command. 'cat', and name
of the file stdout.txt; I'll
see actually contents of stdout.txt
file, but this file is
empty. Let me read the contents of
STDERR file; and there was this
text present; and this text was
produced by STDOUT data stream of
'cat' process;
and if you supply file name as
argument for 'cat' command, you are
not able to send any additional
text via STDIN data stream to 'cat'
process; and the reason for that is
that this file is actually streamed
to STDIN of 'cat' process. That's how
it works under the hood, but STDOUT
is redirected of course to
parent process, and parent process in
this case is bash shell. All right,
now let me demonstrate to you that every
data stream STDIN, STDOUT, and STDERR
has numeric ID. Let me enter
following command 'cat 0<',
and 'stderr.txt',
Enter, and I get same result as before.
[no audio]
And actually in this command
I have told that I want to send
'stderr.txt' file via
standard in to 'cat' process; and
actually this combination '0<'
tells that. '0' is ID of STDIN
data stream, '1' is ID of STDOUT
data stream, and '2' is ID of STDERR
data stream. Let me show you
that. Let me clear terminal; and
let me remove actually those two
files - 'rm stderr.txt', and
'rm stdout.txt'. Now those files
are absent. Great. Let me clear
terminal; and here again I'll use 'ls'
command, it will start 'ls' process,
and I want STDOUT data stream of
this process to be sent to file, and
STDERR data process I'll send to
another file. For that I could use
numeric IDs of data streams - 0,
recap is ID of STDIN, 1 is ID of
STDOUT, and using such syntax
'1>', without space
please, I could send STDOUT data
stream to specific file, or if you
want to other process. I will send to
the file 'stdout.txt'. Next comes
space; and here will be '2>',
2 is ID of STDERR data
stream; and I'll send it to another
file 'stderr.txt', like so.
With this command I will redirect
both STDOUT and STDERR
data streams of 'ls' process. Let's
press Enter; and of course I see
nothing here in the terminal
because STDOUT was redirected to
the file, and here I should see two
newly created files 'stdout.txt',
and 'stderr.txt'; and of
course you could guess that 'stderr.txt'
is empty, and 'stdout.txt'
contains a list of folders, those
folders. Let's have a look at the
'stdout.txt'. 'cat stdout.txt',
here are those folders. And
let's have a look at the 'stderr.txt'.
It's empty. Alright, but if I
repeat same command, but with 'mkdir'
process, let me do that,
I will see that 'stderr.txt'
file now contains this error that
was produced by this process, and
sent via the STDERR data
stream. And please note again that
this 2 is numeric ID of STDERR
data stream. 1 is ID of STDOUT
data stream. Also please notice
that with greater than sign, '>', we
created those files, this one and
this one, each time from scratch. We
rewrite previous file. If you want
to append data to the file, you
could use two arrows instead of
one. Let me show you that. Here will
be greater than sign, '>' like so; and
here will be one more sign as well, '>'.
Let's press Enter, and let's read
contents of error file once again.
And now we see that this text was
appended to this text. Also, let's
repeat the same command, but let's
for example instead of 'mkdir',
let's use 'cat' command, and let's
send also argument to this command.
And this argument will be the name
of the file that is absent now in
this folder where we are running
'cat' command. And in such case when
a file is absent, 'cat' will produce
an error. And let's see which error
it will produce. Let's here type
for example, absent-file.txt,
like so. And this syntax will
remain in place. Let's press Enter,
and let's read contents of 'stderr.txt'
file. And I see that
this new line appeared here in this
text file, 'absent-file.txt: No
such file or directory'; and that is
result of this command. This file
'absent-file.txt' is absent. Here
it is, you don't see this file here
in this folder, that's why 'cat' command
has produced this error, and it has
sent this error via STDERR data
stream to this file. And using two
arrows, '>>', we actually have appended
the contents to existing file.
That's why those lines still
present here in this file, and this
line was appended like so. Alright
guys, that's how you are able to
redirect data streams from a
specific process. And you could
send those streams either to
files as we have tried in this
lecture, or to other processes.
And if you want to send the data
from one process to another, it is
called piping. And let me
demonstrate to you how to perform
piping in the next lecture. I'll
see you there. Bye-Bye.
[no audio]