Path: utzoo!utgpu!cs.utexas.edu!uunet!pdn!tscs!tct!chip From: chip@tct.uucp (Chip Salzenberg) Newsgroups: alt.sources.d Subject: Re: "Simple" but non-portable == useless. Message-ID: <27A5C105.1019@tct.uucp> Date: 29 Jan 91 18:37:56 GMT References: <24078:Jan2516:52:2591@kramden.acf.nyu.edu> <7628@sugar.hackercorp.com> <11306:Jan2817:58:5291@kramden.acf.nyu.edu> Organization: Teltronics/TCT, Sarasota, FL Lines: 49 According to brnstnd@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 , "All this is conjecture of course, since I *only* post in the nude. Nothing comes between me and my t.b. Nothing." -- Bill Coderre