[geeks] ksh for loop question

Greg A. Woods woods at weird.com
Sun May 12 12:59:35 CDT 2002


[ On Saturday, May 11, 2002 at 22:48:22 (-0500), doctor obnox son of a bitch wrote: ]
> Subject: Re: [geeks] ksh for loop question
>
>  "Extraneous CLI noise" figures importantly in my way of working.
>  A good core idea, even "hidden among so much chaff", is still a good
>  core idea. My command line three commands and two pipes. that's "so
>  much chaff"? I agree, the syntax error made it a poor example, but
>  where's the chaff in the core idea, which you admit is "very good"?

The original is what I was talking about, though even any final working
example in that form hides a good part of what you're doing by confusing
the beginner from the very first, unnecessary, word!

>  unix 'cat' will pipe the contents of a file to standard output. That's
>  what I wanted to do. It's efficient from my point of view. One types
>  'cat', then the filename. Maybe the underlying mechanics are repugnant
>  to you, but it works.

No, that's not what "cat" does.  Cat just reads files (or its standard
input) and writes what it reads to its stdandard output.  Oddly enough
that's pretty much what "awk" does too (though of course it leaves out
the ``just'' part).  In fact most unix tools do the same.  There are
almost no ``filters'' that won't accept a list of files on their command
lines and thus "cat" is almost never necessary in shell programming.
The only common counter-example I can think of at the moment is "tr"
(oh, and maybe "dd"), but "tr" it's so rarely used as the leader of a
pipeline that this feature doesn't really go lacking in it.  "Cat" is
sometimes the best way to set a variable's value to the contents of a
file, but it's only real purpose is to join a bunch of files into one
file when no transformation of their content is necessary.

It's the shell that does the pipe fitting, under your command, of course.

Perhaps you should first (re)learn to do things in the most mundane way
so that you really learn the data paths and the manipulations you are
applying.  I.e. always use temporary files and never use "|".

Sadly I once encountered someone who had learned just barely enough
abput shell pipe fitting to make some crude use of it, but unfortunately
they were under the impression they had to use "cat" for every pipe:

	cat somefile | awk '{some awk}' | cat | tr a b | cat | sort

The person who taught them abput pipe fitting was so eager to teach them
neat tricks that they forgot to first teach the fundamentals of what
they were actually doing and why.  Luckily this person was quite
brilliant and was able to unlearn this silliness in almost the blink of
an eye.

>  I do things the New Jersey way: "good enough until proven otherwise".

If that were true then you'd almost never use a pipe.  They didn't even
invent it until they really needed it.

	http://cm.bell-labs.com/cm/cs/who/dmr/mdmpipe.html

(you have to follow the link into the history paper and read the whole
section on "Pipes" to get it all)

>  I'll acheive zenlike shell minimalist perfection someday. Until then,
>  I'll get some work done.

This isn't about perfection or minimalism, and not even exactly about
elegance either -- this is primarily about posting tested examples, not
some random line noise that flew off the ends of your fingers; and
secondarily about writing your examples clearly enough that someone can
actually learn something new from them.

In the mean time practicing perfection and elegance even with one-off
commands (which is espically easy if you like using your command-line
editor) is _always_ a good idea.  What you learn will thus be more
readily available for you to apply when you have to write a production
shell program (i.e. when you have to get some _real_ work done)!  :-)

-- 
								Greg A. Woods

+1 416 218-0098;  <gwoods at acm.org>;  <g.a.woods at ieee.org>;  <woods at robohack.ca>
Planix, Inc. <woods at planix.com>; VE3TCP; Secrets of the Weird <woods at weird.com>



More information about the geeks mailing list