18. Shell I/O Redirection Flashcards
Standard input: stdin
default data source for stdin
is the terminal (text entered at the keyboard)
it is possible to use special BASH shell syntax to change the data source of stdin to come from
somewhere else, such as a file.
Standard output: stdout
default data destination for
stdout is the terminal.
Data written to stdout by a LINUX command executed from a terminal
appears as text in output in the terminal window by default
Standard error: stderr
stderr is an output data stream that programs can write data to.
The default destination for stderr
is the terminal.
Why have standard I/O streams
in LINUX the operating system provides
every program with these streams.
they become much more versatile,When commands use these streams for input and output,
because we can change where that input comes from and where that output goes at the level of the shell without having to modify the source code for the command and recompile it!
standard I/Ostreams are an abstraction of input and output that can be utilized by commands and manipulated
by the shell to achieve more versatile and customizable behavior from commands.
Why have two output streams?
separate actual program output
from diagnostic messages such as errors or warnings.
we can redirect stdout,
and still see the the diagnostic messages from stderr in the terminal.
Redirecting stdin
Redirecting stdout
file descriptor
A file descriptor in the shell is just a number that the operating system assigns to an open I/O stream to or from a file in the filesystem
The file descriptor
of stdin is always 0.
file descriptor for stdout is always 1,
file descriptor for stderr is
always 2.
Redirection of stderr
need to specify the file descriptor 2 with the output redirect
operator,
wc -l < file_list .txt 2> error_log .txt
This would redirect stdin to come from the file file_list.txt, and redirect stderr to the file error_log.txt. In this example, stdout is not redirected and still goes to the terminal
Simultaneously redirecting both stdout and stderr