Return to BSD News archive
Path: sserve!newshost.anu.edu.au!harbinger.cc.monash.edu.au!msunews!uwm.edu!spool.mu.edu!howland.reston.ans.net!swrinde!pipex!uunet!zib-berlin.de!zrz.TU-Berlin.DE!cs.tu-berlin.de!news
From: nickel@prz.tu-berlin.de (Juergen Nickelsen)
Newsgroups: comp.os.386bsd.questions
Subject: Re: FBSD 2.0-950112: Missing "which"
Date: 26 Jan 1995 23:54:55 GMT
Organization: Technical University of Berlin, Germany
Lines: 72
Message-ID: <NICKEL.95Jan27005455@toftum.prz.tu-berlin.de>
References: <3g386a$mfq@Owl.nstn.ca>
Reply-To: nickel@cs.tu-berlin.de
NNTP-Posting-Host: toftum.prz.tu-berlin.de
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
In-reply-to: digdon@Snoopy.UCIS.Dal.Ca's message of 24 Jan 1995 12:00:10 -0400
In article <3g386a$mfq@Owl.nstn.ca> digdon@Snoopy.UCIS.Dal.Ca (Mike
Digdon) writes:
> I tend to use the "which" command quite a lot. Before I had time to install
> bash (I just set up my machine), I had to use csh. "which" seems to be a csh
> built-in, so I was very surprised to find that "which" stopped working once
> I was running bash.
With bash I use a modified version of which defined as a shell
function, presented by itself:
$ which which
which is a function
which ()
{
local tflag;
if [ "$1" = -a ] ; then
tflag=-all;
shift ;
fi;
if [ $# -lt 1 ] ; then
echo usage: which command 1>&2 ;
return 1 ;
fi;
while [ "$1" ];
do
for i in `type -type $1 | uniq`;
do
case $i in
builtin | keyword | function)
type $tflag $1
;;
alias)
type $tflag $1;
type `type $1 |
sed 's/^.*\`\([^ ]*\)[ ].*$/\1/'`
;;
file)
type -path $tflag $1
;;
*)
type $tflag $1
;;
esac;
done;
shift;
done
}
It can do fine things like this:
$ which l
l is aliased to `ls -l'
ls is /bin/ls
or like this:
$ which -a ls
/bin/ls
/usr/bin/ls
/usr/contrib/gnu/bin/ls
Multiple arguments are possible. And since it utilizes the `type'
command, it also handles shell functions, builtins and keywords. If
there are no matches, nothing is printed.
One bug should still be fixed, though: The return code is mostly
meaningless. With bash 1.12.1 it is always 1; with 1.13.1-CWRU it is 0
except when no arguments were given.
--
Juergen Nickelsen