Subject: Re: which(1) is a csh script?
To: Jesper Louis Andersen <jlouis@mongers.org>
From: Greg A. Woods <woods@weird.com>
List: tech-userlevel
Date: 10/06/2003 18:43:48
[ On Monday, October 6, 2003 at 23:43:18 (+0200), Jesper Louis Andersen wrote: ]
> Subject: Re: which(1) is a csh script?
>
> I know that, but assume we are running off /bin/sh and only have
> a POSIX layer. Then what is correct portable way of getting something
> with the same semantics as which(1) or whence in ksh?
The "type" command is almost universally available in all shells
compatible with, or derived from, the original V7 Bourne Shell.
The only "widely" know exception I know about is the original "ash"
which was the basis for the 4.4BSD /bin/sh and indeed NetBSD's /bin/sh
didn't gain a "type" builtin until relatively recently.
I have for quite a long time used the following shell function to
imitate "type" on older systems that still have the original 4.4BSD
/bin/sh or where I might still use /bin/ash:
#
# .ashtype - This should be, but is not, a builtin in /bin/ash
#
# Copyright (c) Greg A. Woods 1996. All rights reserved.
#
# This software is not subject to any license of the American Telephone
# and Telegraph Company, the Regents of the University of California, or
# the Free Software Foundation.
#
# Permission is granted to anyone to use this software for any purpose on
# any computer system, and to alter it and redistribute it freely, subject
# to the following restrictions:
#
# 1. The authors are not responsible for the consequences of use of this
# software, no matter how awful, even if they arise from flaws in it.
#
# 2. The origin of this software must not be misrepresented, either by
# explicit claim or by omission. Since few users ever read sources,
# credits must appear in any relevant documentation.
#
# 3. Altered versions must be plainly marked as such, and must not be
# misrepresented as being the original software. Since few users
# ever read sources, credits must appear in any relevant documentation.
#
# 4. This notice may not be removed or altered.
#
#
#ident "@(#)HOME:.ashtype 26.2 03/03/22 15:50:15 (woods)"
type ()
{
if [ $# -ne 1 ] ; then
echo "Usage: type command" >&2
return 2
fi
functions=`hash | grep "function $1\$"`
if [ -n "$functions" ] ; then
echo "$functions" | sed 's/^\([^ ]*\) \(.*\)$/\2 is a \1/'
unset functions
return 0
fi
unset functions
rc=1
case "$1" in
.|alias|bg|command|echo|eval|exec|exit|export|fg|getopts|hash|jobid|jobs|pwd|read|readonly|return|set|setvar|shift|trap|umask|unset|wait)
typeout="$1 is a builtin"
rc=0
;;
*)
typeout="$1 not found"
oifs="$IFS"
IFS=":"
for pathseg in $PATH
do
if [ -x $pathseg/$1 ] ; then
typeout="$1 is $pathseg/$1"
rc=0
break
fi
done
IFS="$oifs"
unset oifs
;;
esac
echo $typeout
unset hashv pathseg typeout
return $rc
}
--
Greg A. Woods
+1 416 218-0098 VE3TCP RoboHack <woods@robohack.ca>
Planix, Inc. <woods@planix.com> Secrets of the Weird <woods@weird.com>