#!/usr/bin/ksh93
################################################################
function usagemsg_which_k93 {
print "
Program: which_k93
The which_k93 Korn Shell command/function takes a list
of program names and looks for the files that run when
these names are given as commands.
Usage: ${1##*/} [-?] [-q|-v] [-V] CommandName
Where:
'-q' = Quiet mode, path name of command not output
'-v' = Verbose mode
'-V' = Very Verbose Mode
Author: Dana French (dfrench@mtxia.com) Copyright 2004
\"AutoContent\" enabled"
return 0
}
################################################################
function which_k93 {
typeset TRUE="0"
typeset FALSE="1"
typeset QUIET="${FALSE}"
typeset VERBOSE="${FALSE}"
typeset VERYVERB="${FALSE}"
typeset EXITCODE="102"
while getopts ":qvV" OPTION
do
case "${OPTION}" in
'q') QUIET="${TRUE}";;
'v') VERBOSE="${TRUE}";;
'V') VERYVERB="${TRUE}";;
'?') usagemsg_which_k93 "${0}" && return 101;;
esac
done
shift $(( ${OPTIND} - 1 ))
####
#### Check the first command line parameter after processing
#### all the options to see if it is null, if so display the
#### usagemsg and return with an unsuccessful return code.
####
if [[ "_${1}" = "_" ]]
then
usagemsg_which_k93 "${0}"
return 103
fi
(( VERYVERB == TRUE )) && set -x
################################################################
####
#### Separate the directories in the PATH variable into an
#### array. If the PATH variable is empty, return with an
#### unsuccessful return code.
####
[[ "_${PATH}" = "_" ]] && return 104
IFS=":" DIRS=( ${PATH} )
#### Set a pair of variables to keep track of how many
#### command names were not found. Initialize both counters
#### to the number of command name specified on the command
#### line.
EXITCODE="${#}"
CMDS="${#}"
####
#### Loop through each directory of the PATH variable and
#### check for the existance of the specified command name.
#### If it exists, print the fully qualified file name and
#### exit with a successful return code. If it does not
#### exist, don't print anything and return with an
#### unsuccessful return code.
####
for CMDNAME in "${@}"
do
(( CMDS-- ))
for DIR in "${DIRS[@]}"
do
(( VERBOSE == TRUE )) && print "# Searching ${DIR} for ${CMDNAME}"
if [[ -f "${DIR%/}/${CMDNAME}" ]]
then
(( VERBOSE == TRUE )) && print "# Found command \"${CMDNAME}\" in directory \"${DIR}\""
(( QUIET == FALSE )) && print -r -- "${DIR%/}/${CMDNAME}"
(( EXITCODE-- ))
break
fi
done
#### If the number of commands processed does not match the
#### number of commands found, issue an error message for the
#### current command being processed and add 1 back to the
#### number of commands processed.
if (( EXITCODE != CMDS ))
then
print -u 2 "${0}: There is no ${CMDNAME} in ${DIRS[@]}"
(( CMDS++ ))
fi
done
return ${EXITCODE}
}
################################################################
which_k93 "${@}"