"Simple" but non-portable == useless.

Chip Salzenberg chip at tct.uucp
Wed Jan 30 05:37:56 AEST 1991


According to brnstnd at kramden.acf.nyu.edu (Dan Bernstein):
>Be serious, Karl. We're talking about seven simple commands in a
>pipeline versus nearly 30 lines of perl to do the same job: namely, get
>a list of all names where there are two different executables in PATH.

So maybe Karl did it the long way.  Here's my version of pathdup in
Perl, and it's only ten lines long:

========================================================================
for $dir (split(/:/, $ENV{'PATH'})) {
	opendir(DIR, length($dir) ? $dir : ".") || next;
	while (defined($file = readdir(DIR))) {
		next if $file eq "." || $file eq ".." || !-x "$dir/$file";
		$M{$file} = 1 if defined($P{$file});
		$P{$file} .= "\0$dir/$file";
	}
	closedir(DIR);
}
grep(grep(length && (print $_, "\n"), split(/\0/,$P{$_})), sort keys(%M));
========================================================================

I even tested it.  :-)

>It's a hell of a lot simpler to let ls, sort, and uniq do the work than
>to bother traversing directories, stat()ing files, and keeping arrays of
>on thing or another by hand. Surely you agree.

Except that your version also uses rev (what's that?) and BSD-specific
options to ls.  The simplest script in the world is useless to me if
it doesn't work on my system.

In contrast, my Perl script works on all OSs on which readdir() et al
are available.  If readdir() is not available, and if the ability to
distinguish filenames with newlines is not important -- as is apparent
from your willingness to use ls -- then `cd $dir && ls -a` may be used
instead of opendir()/readdir()/closedir().

Your shell script is a marvel of tool usage.  But to me it's a
"dancing bear" script: What's amazing isn't that it's robust and
portable -- it isn't -- but that it works at all.

>Tom knows the shell script is a lot simpler ...

Somewhat simpler (not a lot), but broken for my SysV environment.
"Anything that works is better than anything that doesn't."  --me
-- 
Chip Salzenberg at Teltronics/TCT     <chip at tct.uucp>, <uunet!pdn!tct!chip>
   "All this is conjecture of course, since I *only* post in the nude.
    Nothing comes between me and my t.b.  Nothing."   -- Bill Coderre



More information about the Alt.sources.d mailing list