#!/bin/ksh # @(#) Find command on PATH #..# systems: #..# sites: #..# level: #..# publish: y #..# keys: #..# date: Thu May 6 12:35:52 BST 1999 #..# vers: 6 [ $0 = ${0#/} ] && this=$(pwd -H)/$0 || this=$0 tag=${0##*/} tm=/tmp/${tag} tmp=${tm}$$ rm -f ${tm}* usage="Usage: ${tag} [-ver][-f][-ll|-lf] [-C] [-path] " ver() { eval $1 -ver; } path=$PATH typeset -i f=0 typeset -i i=-1 typeset -i j=0 while [ $# -gt 0 ]; do case "$1" in -x|-v) set "$1" ;; -vi) lockvi $this ; exit ;; -view) view $0; exit ;; -h) echo "$usage" echo "" echo "$tag searches the current PATH for commands" echo "" echo "The argument is used as a pattern to match for command" echo "names in each element of \$PATH. If a match is not" echo "found, it is retried as a partial match. The -f flag" echo "forces a partial match trial even if an exact match is" echo "found." echo "" echo "The '-ll' and '-lf' flags cause an 'ls -ld' and 'ls -F'" echo "listing of each match." echo "" echo "The '-C' flag causes to be executed for each" echo "file found. Useful for 'file', 'more', 'what' ..." echo "" echo "If '-path' is specified, is used instead of" echo "the real PATH. This allows 'what if' checks." echo "" echo "The '-man' flag causes all the subdirectories of the" echo "elements of \$MANPATH to be searched." echo "" echo "The '-s' flag causes the script to return with status 0" echo "if the first non-flag argument is found unmodified and" echo "with status 1 if not." echo "" exit ;; -about) echo "$tag was written by David Ledger" echo "dledger@ivdcs.co.uk" echo "Free to use at your own risk" echo "Copyright 2004" exit ;; -f) f=-1 ;; -s) stat=1 ;; -fun*) path="" for d in $(echo "$FPATH" | tr ":" " "); do n=$(echo $d/* | tr " " ":") path="${path-}${path+:}$n" done path=${path#:} unset d n ;; -man) path="" for d in $(echo "$MANPATH" | tr ":" " "); do n=$(echo $d/* | tr " " ":") path="${path-}${path+:}$n" done path=${path#:} unset d n ;; -ver) [ $# -le 1 ] && { ver=$(egrep '#..#[ ]*vers:' $this) ver=${ver#"${ver%%[1-9]*}"} print ${ver%%[!0-9]*} exit } || i=i+1; c[$i]="ver" ;; -ll) i=i+1; c[$i]="ls -l" ;; -lL) i=i+1; c[$i]="ls -Ll" ;; -lf) i=i+1; c[$i]="ls -F" ;; -file) i=i+1; c[$i]="file" ;; -wc) i=i+1; c[$i]="wc -c" ;; -[1-9]) l=${1#-} ;; -C?*) i=i+1; c[$i]="${1#-C}" ;; -path*) path=${1#-path} ;; -[Oo][Pp][Tt]*) path="$OPTPATH" ;; +[Oo][Pp][Tt]*) path="$PATH:$OPTPATH" ;; *) [ ${#c[*]} -eq 0 ] && { i=0; c=echo; } [ $f -ge 0 ] && f=1 for d in `echo "$path" | tr ":" " "`; do [ -r $d ] || continue set -A p $d/$1 [ -f $p ] && f=$((f * 2)) && { [ ${stat-0} -eq 1 ] && exit 0 j=0 while [ $j -le $i ]; do eval ${c[$j]} $d/$1 j=j+1 done } done [ ${stat-0} -eq 1 ] && exit 1 [ $f -gt 1 ] && shift && continue for d in `echo "$path" | tr ":" " "`; do [ -r $d ] || continue for n in `ls $d | grep ${case:-} "$1"`; do j=0 while [ $j -le $i ]; do eval ${c[$j]} $d/$n j=j+1 done done done ;; esac shift done [ "$d" ] || echo "${path-$PATH}" | tr ":" "\012"