--------------
     Pipes and
   Redirection
--------------

Someone wrote:
: OK...I thought I had it all straight in my mind but 
: I now find the difference between redirection and
: pipes really foggy.  Is there something absolute I 
: can put in my mind to clarify the two?

EXCELLENT question!!!

--------------
        Digest
--------------

Redirection is a mechanism in the shell that causes the standard input for
a program to come from a file rather than from the terminal. It also
causes the standard output and standard error to go to a file rather than
to the terminal. (Standard output, standard error? Later lesson!)

Input redirection is symbolized by the < character. Output is the >
character, and output with append uses the >> characters.

A pipe is a mechanism used by one command to pass information to a second
command for processing; a pipe connects the standard output of one command
to the standard input of the next, without creating an intermediate file.

A pipe is symbolized by the vertical bar | character.

--------------
      In-depth
--------------

I don't know of any easy way to learn the difference, other than to
practice and see what works:

  $ echo this > thisfile
  $ cat thisfile
  this
  $

We have redirected the output of echo to a file. We can then cat the file
and display what is in it. Now try piping from echo to a file:

  $ echo this | thisfile
  -ksh: thisfile: cannot execute [Permission denied]
  $

As you can see, this does not work. However, we can pipe from one command
to another:

  $ echo this | cat
  this
  $ 

This is a redundant operation. It would win the "useless use of cat" award
(more about that later). However, it works without generating an error
(important for our purposes), and this next step proves that *echo* isn't
displaying "this" to the screen, *cat* is.

Now lets try to redirect to cat and see what happens:

  $ echo this > cat
  $

Well, what happened to our output? It didn't appear on the screen! We can
find it if we look for redirection to a file using cat:

  $ cat cat
  this
  $

Aha! In the previous command we redirected the string "this" to a
newly-created file called cat, and we can display that file with cat.

As a general guideline, then, redirection seems to work with files, and
piping works with commands. It does get a bit more complicated later on,
but for now that is a good way to tell the difference. 

Lets try one more example. The wc command does word counting, and this
particular behavior is turned on with the -w switch. Thus:

  $ echo this | wc -w
         1
  $

We have echoed one word to wc -w.

  $ echo this that | wc -w
         2
  $

  $ echo this that other | wc -w
         3
  $

...and a two, and a three...

Now lets try redirecting three words to wc -w (instead of piping).
 
  $ echo this that other > wc -w
  $  

No output. No error. That means our command was successful, but maybe
didn't do what we wanted (since we were expecting output to the screen).

We can find the file created with redirection by using ls:

  $ ls
  wc
  $ 

And now for the mysterious part:

  $ cat wc         
  this that other -w
  $ 

Our words were not counted, they were redirected into a file which has
the filename wc.

But how did that -w get in there? Ahhh, but that is the subject of a later
lesson!

  _______________________________ end ___________________________     

Errata: it IS possible to pipe to a file. How? Because (as we saw in the
last lesson) a file can become a command.

But piping to a user-created command is an advanced technique, and we
aren't going to explore that right away.


best wishes,

richard myers

Reply via email to