Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Describe all Unix apps in current PATH' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Describe all Unix apps in current PATH
Authored by: merlyn on Jan 09, '07 08:15:43AM
Oh wait, that's not hella faster, because it still invokes whatis repeatedly (and with a whitespace problem). This is hella faster:

#!/usr/bin/env perl
use strict;
$|++;

my %commands;
for my $dir (split /:/, $ENV{PATH}) {
  next unless opendir DIR, $dir;
  for my $command (readdir DIR) {
    $commands{$command}++ if -f "$dir/$command" and -x _;
  }
}

open WHATIS, "-|", "whatis", sort keys %commands or die;
while (<WHATIS>) {
  my ($key, $detail) = split /\s+- / or next;
  next unless $key =~ s/\((1|1m|6|8)\)//g;
  printf "%20s - %s", $key, $detail;
}


[ Reply to This | # ]
Describe all Unix apps in current PATH
Authored by: gshenaut on Jan 09, '07 08:57:51AM
Well, formatting helps a little:
echo $PATH \
| sed -e 's/:/ /g' \
| xargs -J % find % -maxdepth 1 \( -type f -or -type l \) \
| xargs basename | sort | uniq \
| xargs whatis 2> /dev/null \
| grep -E '\((1|1m|6|8)\)' \
| perl -ne '($name, $descrip) = m/^(.*?)\s+- (.*)$/; $name =~ s/\((1|1m|6|8)\)//g; printf("%-20s - %s\n", $name, $descrip)'
Greg Shenaut

[ Reply to This | # ]
Sorry, bash wins.
Authored by: bm on Jan 25, '07 11:46:07PM
merlyn:
That's not faster at all. Even with an added command in bash (see below), 'time' reports:
  bash
real    0m44.014s
user    0m11.898s
sys     0m25.316s

  perl
real    0m55.462s
user    0m0.302s
sys     0m0.172s
One caveat: that's bash 3.2.9 -- the latest, while perl remains at 5.8.7;
..the bash-code used an additional 'sed' to escape whitespacing in $PATH.

[ Reply to This | # ]