########################################################################## # Title : search - search files in directory list # Author : Heiner Steven # Date : 1994-03-10 # Category : FILE Utilities # Requires : # SCCS-Id. : @(#) search 1.2 02/12/19 ########################################################################## # Changes # 17.05.94 stv handle wildcards in file names (0.2) # 30.05.95 stv print each path found in seperate line (0.3) # 27.02.96 stv minor changes (0.4) ########################################################################## # 12 Aug 2007 Gilbert Ashley # removed dependency on sed for character replacement, using shell functions instead # removed usage of getopts and basename, using shell functions instead PN=${0##/*} # Program name VER='1.3' show_usage () { echo "$PN - search files in directory list, $VER (stv '94) show_usage: $PN [-v variable_name] [-a] [file ...] -v: environment variable containing search path (default is PATH) -a: print all matches (default: print only first match) The environment variable may contain directory names, separated with ':'." >&2 exit 1 } show_version() { echo ${0##/*}" (BashTrix) $VERSION" echo "Original version by Heiner Steven " echo "Modified to remove external dependencies by Gilbert Ashley" echo "Copyright 2007 Gilbert Ashley " echo "This is free software written in pure shell." exit } ignore() { set -- `getopt 'hav:' "$@"` VAR=$'PATH' # Variable name ALL=NO # Show all matches (y/n)? while [ $# -gt 0 ] do case "$1" in -v) VAR="$2"; shift;; -a) ALL=YES;; --) break;; -*|-h) show_usage ;; esac shift done shift # terminating '--' from getopt } VAR='PATH' # Variable name ALL=NO for WORD in "$@" ; do case $WORD in -v) VAR="$2"; shift 2;; -a) ALL=YES ; shift ;; --) break;; ""|-*|-h|--help) show_usage ;; --version) show_version ;; esac done DIRS=`eval echo '$'$VAR` # Contents of environment variable # Replace all separators ':' with blanks. # Special handling: # ':' at the beginning or at the end of the list means "current directory" # DIRS=`echo "$DIRS" | sed 's/::/:.:/g;s/^:/.:/;s/:$/:./;s/:/ /g'` # Gilbert rewrites the above into shell # translate leading ':' to '. ' (dot space) [[ "${DIRS:0:1}" = ":" ]] && DIRS=". "${DIRS:1} # translate trailing ':' to ' .' (space dot) OFFSET=$(( ${#DIRS} - 1 )) [[ "${DIRS:$OFFSET:1}" = ":" ]] && DIRS=${DIRS:0:$OFFSET}" ." # translate remaining ':' to ' ' (space) DIRS=${DIRS//:/ } # Search all files in all given directories RESULT=1 # Result (nothing found) CWD=`pwd` for FILE do for DIR in $DIRS do [ "$DIR" = "." ] && DIR="$CWD" set -- $DIR/$FILE # May be more than one file for Path do if [ -f "$1" ] then RESULT=0 # At least one file found echo "$Path" [ $ALL = NO ] && break 3 # Search all files? fi done done done exit $RESULT