ugh.book

(singke) #1
Pipes 161

programs to be built out of simpler programs. Pipes allow programs to be
used in unplanned and unanticipated ways. Pipes allow simple
implementations.” Unfortunately, chanting mantras doesn’t do Unix any
more good than it does the Hari Krishnas.


Pipes do have some virtue. The construction of complex systems requires
modularity and abstraction. This truth is a catechism of computer science.
The better tools one has for composing larger systems from smaller sys-
tems, the more likely a successful and maintainable outcome. Pipes are a
structuring tool, and, as such, have value.


Here is a sample pipeline:^3


egrep '^To:|^Cc:' /var/spool/mail/$USER | \
cut -c5- | \
awk '{ for (i = 1; i <= NF; i++) print $i }' | \
sed 's/,//g' | grep -v $USER | sort | uniq

Clear, huh? This pipeline looks through the user’s mailbox and determines
which mailing lists they are on, (well, almost). Like most pipelines, this
one will fail in mysterious ways under certain circumstances.


Indeed, while pipes are useful at times, their system of communication
between programs—text traveling through standard input and standard out-
put—limits their usefulness.^4 First, the information flow is only one way.
Processes can’t use shell pipelines to communicate bidirectionally. Second,
pipes don’t allow any form of abstraction. The receiving and sending pro-
cesses must use a stream of bytes. Any object more complex than a byte
cannot be sent until the object is first transmuted into a string of bytes that
the receiving end knows how to reassemble. This means that you can’t
send an object and the code for the class definition necessary to implement
the object. You can’t send pointers into another process’s address space.
You can’t send file handles or tcp connections or permissions to access
particular files or resources.


At the risk of sounding like a hopeless dream keeper of the intergalactic
space, we submit that the correct model is procedure call (either local or
remote) in a language that allows first-class structures (which C gained
during its adolescence) and functional composition.


(^3) Thanks to Michael Grant at Sun Microsystems for this example.
(^4) We should note that this discussion of “pipes” is restricted to traditional Unix
pipes, the kind that you can create with shell using the vertical bar (|). We’re not
talking about named pipes, which are a different beast entirely.

Free download pdf