|
"Wizard's Grabbag": Column No. 002: Listing |
#!/bin/sh
# @(#) wh which-like command with extended functions
# Author: J. Blake 01-20-94
#
# Initialize local variables:
# These variables determine default behavior:
Dir_only=FALSE # Don't display parent directory
All=FALSE # Display all occurrences in path
File=FALSE # Don't display file type
List=FALSE # Don't display long directory listing for file
Name=TRUE # Display file name
Substring=FALSE # Only match as complete string
# Flag variable:
Notfound="" # Keeps track of files not found
# Define function to display correct command-line usage and exit:
usage_exit() {
cat << EOF >&2
Usage: `basename $0` [-a] [-d] [-f] [-l] [-s] command-name...
( -a print all instances in path )
( -d print directory path only )
( -f print type of file )
( -l print ls -l for file )
( -s use case-sensitive substring match )
EOF
exit 1
}
# Define function to display information on standard output:
printinfo()
{
if [ "$Name" = "TRUE" ]; then
echo $1 # display name
fi
if [ "$File" = "TRUE" ]; then
file $1 # display file type
fi
if [ "$List" = "TRUE" ]; then
ls -l $1 # display long directory listing for file
fi
}
# Process command-line options:
while : # check all options
do
case "$1" in
-d) Dir_only=TRUE # list only the directory
shift
continue ;;
-a) All=TRUE # list all occurrences
shift
continue ;;
-f) File=TRUE # display the type of file
Name=FALSE # don't display the file name
shift
continue ;;
-l) List=TRUE # display long listing for the file
Name=FALSE # don't display the file name
shift
continue ;;
-s) Substring=TRUE # search for a substring
All=TRUE # look for all occurrences
shift
continue ;;
-*) echo "Invalid option \"$1\"" >&2
usage_exit ;; # Display correct usage, abort
*) break ;; # Go check remaining arg count
esac
done
# Make sure at least one command-line argument remains:
case $# in
0) echo "Must specify at least one command name" >&2
usage_exit ;; # display correct usage and exit
esac
# Reformat value of PATH environment variable:
# Replace any initial colon by dot-colon, any final colon by
# colon-dot, any double colon by dot-colon, then translate all
# colons to a single space character storing resulting directory
# list in the local Dirs variable:
#
Dirs=`echo $PATH |
sed -e 's/^:/\.:/' -e 's/:$/:\./' -e 's/::/:\.:/' -e 's/:/ /g'`
# Process non-option arguments:
#
while [ $# -gt 0 ]; do # for all remaining command-line arguments
Found=FALSE # set initial condition (not found)
for dir in $Dirs # for all directories in shell PATH
do
if [ "$Substring" = "TRUE" ]; then
FileS=`ls -1a $dir | \grep "$1"` # all files with substring
else
FileS=$1 # set FileS to file name
fi
for file in $FileS; do # do for all files found
if [ -f $dir/$file ]; then
if [ "$Dir_only" = "TRUE" ]; then
printinfo $dir # print the directory info
else
printinfo $dir/$file # print the file info
fi
Found=TRUE
if [ "$All" = "FALSE" ]; then
break 2 # jump out
fi
fi
done
done
if [ "$Found" = "FALSE" ]; then # no match found in path
Notfound="$Notfound $1" # add to not-found list
fi
shift
done
if [ "X$Notfound" != "X" ]; then # empty not-found list?
echo No $Notfound in $Dirs # Display failure message
fi
exit 0
|
Print This Page Send as e-mail |












