#! /bin/sh # apropos-command --- display list of commands in path matching pattern # Author: Noah Friedman # Created: 1997-05-27 # Public domain. # $Id: apropos-command,v 1.5 2006/09/22 21:54:05 friedman Exp $ # Reinvoke with perl if available. { ${PERL-perl} -v; } > /dev/null 2>&1 && exec ${PERL-perl} -Sx "$0" ${1+"$@"} # bourne shell; portable, but much slower and less functional. # It may even fail if the `-x' test flag is not supported. { for dir in `echo "$PATH" | sed -e 's/^:/.:/;s/::/:.:/g;s/:$/:./;s/:/ /g'` do ls -1a "$dir" \ | grep -i "$1" \ | while read f; do test -f "$dir/$f" && test -x "$dir/$f" && echo "$f" done done | sort -u exit $? } #!perl [perl will skip all lines in this file before this line] { # Cheesy option parsing $filterp = 1; while (substr ($ARGV[0], 0, 1) eq '-') { my $arg = shift @ARGV; last if ($arg eq '--'); $filterp = 0 if ($arg eq '-a'); } my $pattern = shift @ARGV; my @pathlist = split (/:/, $ENV{PATH}); my %files; for my $dir (@pathlist) { $dir = "." if ($dir eq ""); opendir (D, $dir) || next; for my $file (grep (/$pattern/io, readdir (D))) { if ($filterp) { # Skip shared libraries (which are often marked executable) # unless user specifically put `lib' in the search pattern next if ($file =~ /^lib.*\.(?:a|so|la)/o && $pattern !~ /lib/o); # Same for windows/cygwin dlls next if ($file =~ /\.dll(?:\.|$)/o && $pattern !~ /\.dll/o); # Skip backup and autosave files unless explicitly searched next if ($file =~ /[~#]$/o && $pattern !~ /[~#]/o); } my $f = join ("/", $dir, $file); # `_' reuses previous stat struct, saves syscall $files{"$file\n"} = undef if (-f $f && -x _); } closedir (D); } print sort { uc($a) cmp uc($b) } keys %files; } # local variables: # mode: perl # eval: (auto-fill-mode 1) # end: # apropos-command ends here