[H-CHAT] Re: [H-GEN] Encrypting a tar backup

Greg Black gjb at gbch.net
Wed Oct 23 20:35:53 EDT 2002


[ Humbug *General* list - semi-serious discussions about Humbug and     ]
[ Unix-related topics. Posts from non-subscribed addresses will vanish. ]

Jason Parker-Burlingham wrote:

| In that case, I'll elaborate for anyone who doesn't already know (all
| this can be found by searching for the "useless use of cat" award).
| 
| Whenever you see
| 
|         $ cat file | program
| 
| you can usually use
| 
|         $ program file

Well, the pedants would say that example 2 should be:

    $ program < file

| While the efficiency argument is a bit of a furphy, seeing someone
| overusing cat usually means you can rely on them to do things the hard
| way at other times, too:
| 
|         $ WRONG=`cat file | grep pattern | wc -l`
|         $ RIGHT=$(grep -c pattern file)

Here again, I would disagree.  It's not grep's job to count
stuff and that's better left to the specialised program, wc.
The second part really should be:

    $ RIGHT=`grep pattern < file | wc -l`

For more on this, see Rob Pike's classic paper "UNIX Style, or
cat -v Considered Harmful", USENIX Summer Conference
Proceedings, 1983.  A later paper that expands on it called
"Program Design in the UNIX Environment" by Kernighan and Pike
can be found here: 

    http://www.cs.bell-labs.com/cm/cs/doc/84/kp.ps.gz

Every Unix user should read and understand it.

And it's worth noting that Pike states that using cat, even for
a single file, makes sense -- read the paper for his very lucid
explanation.

| What's more interesting is working out where cat *is* useful.

In that context, it's worth reminding people what "cat" means.
Ever since the beginning of time, it has been described in the
man page as: "concatenate and print files"[1].  Here, print is
just a term that really means "deliver to standard output".  The
real information is in the word that gave the program its name.
The intention is to provide a utility that concatenates files.

This has two uses: for a true filter that reads from standard
input and has no capability of dealing with named files, cat can
deliver a set of files to such a tool.  And, for tools like grep
which can handle named files, you can still accomplish different
things by using cat from what you get with grep alone:

    $ grep pattern foo bar baz

is not the same as 

    $ cat foo bar baz | grep pattern

Part of learning about shell programming (and command line use
in general) is understanding when you might want to use cat and
when you're better off not using it.[2]

Personally, I don't care if people use:

    $ cat foo | program

Often, they are working up to:

    $ cat foo bar baz | program

And, in this case, it's easier to paste in the extra names than
to reconstruct the command once the initial test has been run.
And, as Rob Pike says in the article referred to above, there is
nothing wrong with "cat foo | program" anyway.  There are far
more important things to weed out of one's repertoire.


Notes:

[1] I think it was really just "concatenate and print" in the
    beginning, but the explanatory "files" was added in a long
    time ago.

[2] If people would like me to expand on any of this in a talk
    at a meeting, let me know -- or just grab me at a meeting to
    argue the toss.

Greg

--
* This is list (humbug) general handled by majordomo at lists.humbug.org.au .
* Postings to this list are only accepted from subscribed addresses of
* lists 'general' or 'general-post'.  See http://www.humbug.org.au/



More information about the General mailing list