[gnu.bash.bug] Bash function to do ksh `whence'

chet@cwns5.INS.CWRU.Edu (Chet Ramey) (08/05/89)

The following shar file contains two versions of the Korn Shell command
`whence'.  The first (whence) is a strict implementation; the second
(whence2) does a little less checking.  Send opinions to me.

Chet Ramey			"We are preparing to think about contemplating 
Network Services Group, CWRU	 preliminary work on plans to develop a
chet@cwjcc.INS.CWRU.Edu		 schedule for producing the 10th Edition of 
				 the Unix Programmers Manual." -- Andrew Hume

#! /bin/sh
# This is a shell archive, meaning:
# 1.  Remove everything above the #! /bin/sh line.
# 2.  Save the resulting test in a file
# 3.  Execute the file with /bin/sh (not csh) to create the files:
#
#                whence
#                whence2
#
# Created by chet on Fri Aug  4 21:36:23 EDT 1989
#
if test -f 'whence'
then
echo shar: will not over-write existing file "'whence'"
else
echo extracting "'whence'"
sed 's/^X//' >whence <<'SHAR_EOF'
X#
X# An almost-ksh compatible `whence' command.  This is as hairy as it is 
X# because of the desire to exactly mimic ksh.
X# 
X# This depends somewhat on knowing the format of the output of the bash
X# `builtin type' command.
X#
X# Chet Ramey
X# chet@ins.CWRU.Edu
X#
X
X
Xwhence()
X{
X	local vflag
X	local path
X
X	vflag=
X	path=
X	if [ "$#" = "0" ] ; then
X		echo "whence: argument expected"
X		return 1
X	fi
X	case "$1" in
X		-v) vflag=1
X		    shift 1
X		    ;;
X		-*) echo "whence: bad option: $1"
X		    return 1
X		    ;;
X		 *) ;;
X	esac
X
X	if [ "$#" = "0" ] ; then
X		echo "whence: bad argument count"
X		return 1
X	fi
X
X	for cmd
X	do
X		if [ "$vflag" ]	 ; then
X			echo $(builtin type $cmd | sed 1q)
X		else
X			path=$(builtin type -path $cmd)
X			if [ "$path" ] ; then
X				echo $path
X			else
X				case "$cmd" in
X					/*) echo ""
X					    ;;
X					 *) case "$(builtin type -type $cmd)" in
X						"") echo ""
X						    ;;
X						 *) echo "$cmd"
X						    ;;
X					    esac
X					    ;;
X				esac
X			fi
X		fi
X	done
X	return 0
X}
SHAR_EOF
if test      985 -ne "`wc -c < 'whence'`"
then
echo shar: error transmitting "'whence'" '(should have been      985 characters)'
fi
fi
if test -f 'whence2'
then
echo shar: will not over-write existing file "'whence2'"
else
echo extracting "'whence2'"
sed 's/^X//' >whence2 <<'SHAR_EOF'
X#
X# An almost-ksh compatible `whence' command.  The one difference between this
X# and the real ksh `whence' is that this version, when the "-v" flag is not
X# given and presented with something that is not in the path, simply echos it
X# without more checking to see if it is a function, builtin, or alias.
X#
X# Chet Ramey
X# chet@ins.CWRU.Edu
X#
X
X
Xwhence()
X{
X	local vflag
X	local path
X
X	vflag=
X	path=
X	if [ $# = "0" ] ; then
X		echo "whence: argument expected"
X		return 1
X	fi
X	case "$1" in
X		-v) vflag=1
X		    shift 1
X		    ;;
X		-*) echo "whence: bad option: $1"
X		    return 1
X		    ;;
X		 *) ;;
X	esac
X
X	if [ $# = "0" ] ; then
X		echo "whence: bad argument count"
X		return 1
X	fi
X
X	for cmd
X	do
X		if [ "$vflag" ]	 ; then
X			echo $(builtin type $cmd | sed 1q)
X		else
X			path=$(builtin type -path $cmd)
X			if [ "$path" ] ; then
X				echo $path
X			else
X				case "$cmd" in
X					/*) echo ""
X					    ;;
X					*) echo "$cmd"
X					   ;;
X				esac
X			fi
X		fi
X	done
X	return 0
X}
SHAR_EOF
if test      957 -ne "`wc -c < 'whence2'`"
then
echo shar: error transmitting "'whence2'" '(should have been      957 characters)'
fi
fi
# end of shell archive
exit 0

-- 
Chet Ramey			"We are preparing to think about contemplating 
Network Services Group, CWRU	 preliminary work on plans to develop a
chet@cwjcc.INS.CWRU.Edu		 schedule for producing the 10th Edition of 
				 the Unix Programmers Manual." -- Andrew Hume