[mdlug] UNIX tips: Learn 10 good UNIX usage habits

Aaron Kulkis akulkis3 at hotpop.com
Mon Mar 10 14:54:54 EDT 2008


Robert Citek wrote:
> On Mon, Mar 10, 2008 at 2:19 AM, Michael Corral <micorral at comcast.net> wrote:
>> 2008-03-10, Monsieur Robert Citek a ecrit:
>>
>>> contrast the output of these commands:
>>> $ cat /etc/passwd /etc/group | grep root
>>  > $ grep root /etc/passwd /etc/group
>>
>>  grep -h root /etc/passwd /etc/group
>>
>>  That will give the same output as the cat+pipe version.
>>  So calling cat is unnecessary in this example, since the one grep
>>  command does the same as two commands and a pipe.
> 
> Agreed.
> 
> But my point was about not taking that rule (or any of the rules) as
> dogma.
 >  Sometimes maintaining a pattern is more important than
> execution efficiency.  In many cases, I find the pattern "$prog1
> $files | $prog2" easier and more consistent to work with.  So I'll use
> it, regardless if $prog1 is cat or if $files expands to only one file.

That's small-minded.

Using
  $ cat file | command -flags | command2
to accomplish
  $ command -flags < file | command2
is just ludicrous.

And if it's that hard to read, then do it like this:
$ <file command -flags | command2


You write a shell script to do some task...

And then someone else writes a shell-script, which has a giant
for loop and calls your script in their script....

It's ridiculous cpu cycle waste...
An unnecessary fork() + exec()... all for the purpose of
reading a file, and moving the bytes from one buffer to
another to another, when the same thing can be accomplished
by simply using the facility which was specifically designed
to do that with far less over-head: namely file redirection.

> 
> Besides, if I'm worrying about execution efficiency, I should probably
> be writing the program in C and not in shell.

You never know how your code is going to be re-used by someone else.
Introducing NEEDLESS inefficiency is silly.

And there's not always an advantage to writing in C.
For one, you have to debug whatever you're doing, whereas
the building blocks of a shell-script consist of
well-debugged code.

Replicating an AWK or SED expression in C is not always
a trivial matter.





More information about the mdlug mailing list