coreutils: I/O redirection

1 
1 I/O Redirection
1 ===============
1 
1 Hopefully, you are familiar with the basics of I/O redirection in the
1 shell, in particular the concepts of “standard input,” “standard
1 output,” and “standard error”.  Briefly, “standard input” is a data
1 source, where data comes from.  A program should not need to either know
1 or care if the data source is a disk file, a keyboard, a magnetic tape,
1 or even a punched card reader.  Similarly, “standard output” is a data
1 sink, where data goes to.  The program should neither know nor care
1 where this might be.  Programs that only read their standard input, do
1 something to the data, and then send it on, are called “filters”, by
1 analogy to filters in a water pipeline.
1 
1    With the Unix shell, it’s very easy to set up data pipelines:
1 
1      program_to_create_data | filter1 | ... | filterN > final.pretty.data
1 
1    We start out by creating the raw data; each filter applies some
1 successive transformation to the data, until by the time it comes out of
1 the pipeline, it is in the desired form.
1 
1    This is fine and good for standard input and standard output.  Where
1 does the standard error come in to play?  Well, think about ‘filter1’ in
1 the pipeline above.  What happens if it encounters an error in the data
1 it sees?  If it writes an error message to standard output, it will just
1 disappear down the pipeline into ‘filter2’’s input, and the user will
1 probably never see it.  So programs need a place where they can send
1 error messages so that the user will notice them.  This is standard
1 error, and it is usually connected to your console or window, even if
1 you have redirected standard output of your program away from your
1 screen.
1 
1    For filter programs to work together, the format of the data has to
1 be agreed upon.  The most straightforward and easiest format to use is
1 simply lines of text.  Unix data files are generally just streams of
1 bytes, with lines delimited by the ASCII LF (Line Feed) character,
1 conventionally called a “newline” in the Unix literature.  (This is
1 ‘'\n'’ if you’re a C programmer.)  This is the format used by all the
1 traditional filtering programs.  (Many earlier operating systems had
1 elaborate facilities and special purpose programs for managing binary
1 data.  Unix has always shied away from such things, under the philosophy
1 that it’s easiest to simply be able to view and edit your data with a
1 text editor.)
1 
1    OK, enough introduction.  Let’s take a look at some of the tools, and
1 then we’ll see how to hook them together in interesting ways.  In the
1 following discussion, we will only present those command line options
1 that interest us.  As you should always do, double check your system
1 documentation for the full story.
1