UnixWorld Online: ``Wizard's Grabbag'': Column No. 002

Which, Where, What Is It?

This month we feature Jeffrey Blake's enhanced version of the BSD which command implemented as a Bourne shell script.

Dear Editor:

I just love scripts and finding new ways to use them. My contribution is a little script that is particularly nice because I can use it in combination with other Unix commands inside an alias. It is simple and powerful (one might even say elegant).

My wh script is used to locate the first or all occurrences (-a option) of a file in your shell's command-search path. The other options let you display additional information, such as file type (determined by the file command when you specify the -f option) and long-directory listing (specified with -l option to wh). Additionally, it can display the list of files that match a substring (-s option). I've also found the -d option helpful when I wanted to reference the directory containing the located command. The name ``wh'' might be considered shorthand for which, where, or what.

Now wh started life as a simple script to find a file in my command-search path. I added features as the need arose or I had a new idea. Then, I began looking for ways to use wh in combination with other commands. First, I combined wh with vi inside an alias to give me the ability to quickly start up an editing session for a file in my bin directory without specifying the path name to the file. Other aliases soon followed. The C shell aliases that I use currently include:

alias fcd  'cd `wh -d \!*`;ls'
alias fvi  'vi `wh \!*`'
alias fcp  'echo copying `wh \!*` to current directory..;cp -i `wh \!*` .;'
alias fpd  'pushd `wh -d \!*`;ls' 

The ``f'' in ``fvi'' stands for find-the-file and ``vi'' it. In retrospect, I could have named it ``wvi'' but I don't like the way it looks. Consequently the names of all my other aliases using wh also begin with a lower-case ``f.''

Jeffrey Blake / La Jolla, California / jblake@nosc.mil

Editor's Note: Here are some sample command-line usage examples:

% wh
Must specify at least one command name
Usage: wh [-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  )
% wh date
% wh -a date
% wh -d date
% wh -f date
/bin/date:	sparc pure dynamically linked executable
% wh -l date
-rwxr-xr-x  1 root         7456 Apr 15  1991 /bin/date
% wh -s date
% wh -a -f date
/bin/date:	sparc pure dynamically linked executable
/usr/bin/date:	sparc pure dynamically linked executable
/usr/5bin/date:	sparc demand paged dynamically linked executable
% wh -a -l date
-rwxr-xr-x  1 root         7456 Apr 15  1991 /bin/date
-rwxr-xr-x  1 root         7456 Apr 15  1991 /usr/bin/date
-rwxr-xr-x  1 root        16384 Apr 15  1991 /usr/5bin/date
% wh -d -f date
/bin:		symbolic link to usr/bin
% []

Copyright © 1995 The McGraw-Hill Companies, Inc. All Rights Reserved.
Edited by Becca Thomas / Online Editor / UnixWorld Online / beccat@wcmh.com

[Go to Content] [Search Editorial]

Last Modified: Wednesday, 23-Aug-95 16:56:02 PDT