|
Recall that Unix is very closely related to the C programming
language, thus it inherits many of the features of C. Within
C there are three open streams (which by the way are
implemented as FILE pointers) with constant pointers called
stdin, stdout, and stderr. The std part of each of these
stands for standard, in, out, and err parts stand for input,
output and error respectively.
Stdin
Stdin is where programs get their input from, this is
generally your keyboard, but as you will see below, this can
be changed. Stdout is where programs write there normal
output to, that is, programs that want to write things to the
screen or have it be redirected to a file or another program.
Along the same lines as stdout, is stderr: stderr is
generally where programs write diagnostics or errors to.
While the output is generally intended to go to the screen,
it is implemented differently than stdout, by conceptually
using a different device, which has the effect of not
allowing errors to be redirected along with all normal output
(which again, can be changed).
Redirection
Redirection allows a user to
specify a different device/file for stdin, stdout and stderr.
This allows you to send the output of a program to a file
instead of to the screen, and allows input that is usually
typed in from the keyboard to come to from a file.
You specify IO redirection by placing a greater than (>)
or less than (<) symbol and a file after a command. For
example:
command < inputfile > outputfile
The '<' character causes the standard input to come from
the file. Specifically, it causes the shell to temporarily
substitute what stdin normally points to with a pointer to
n the file. When all the contents of the file have been fed to
the program, the program is handed an end of file marker. If
the program still wants input after the end of file marker,
it will be taken from the keyboard.
Using the '>' character causes all standard output of the
command to go to the file. Any input will still come from
standard input, however you will not see any prompts for
information (that is, any questions the program asks you via
standard output, will be sent to the file so you will not see
them). When you redirect output to a file using >, it will
create the file if it does not exist. If the file does exist,
it is truncated to a zero length. Redirecting standard input
will not trap any errors sent to standard error (errors and
warnings from compilers for example).
>> works the same as >, with the exception
that the output is appended if the file already exists. Using
<< is a bit different than you might think, <<
indicates to the shell that it should collect input from the
user and use that input as input to the program (instead of
the command getting the user input, it is handed what the
shell collected). The string following the << is used
as the stopping point for the shell, that is the shell will
read input until it sees a word matching the one following
<<. This is very useful in shell scripts, since the
shell performs command and variable substitution on the input
before it is handed to the program.
>& and >>& work exactly like their
respective counterparts > and >>, with the exception
that standard error is also passed to the file along with
stdout. There is no way using csh or tcsh to direct only
stderr to a file.
Note: Due to the way Unix does buffering
and the fact that stderr and stdout are different output
devices, output from stderr and stdout might not appear in
the proper order. If a stdout line is printed first, and a
stderr line right after it in a program, the stderr line will
likely show up before the stdout line.
|