UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 4.9 Setting Up vi with the .exrc File Chapter 4
Organizing Your Home Directory
Next: 5. Setting Up Your Terminal
 

4.10 Find All Command Versions with whereiz

To get the absolute pathname of a command, Korn shell users can run whence. bash users have type. On other shells, use which (50.8). But those will only show the first directory in your PATH (6.4) with that command. If you want to find other commands with the same name in other directories, the standard which won't show them to you. (The which on the CD-ROM will - if you use its  - a option. So will the bash command type -all.) whereiz will:

% which grep
/usr/bin/grep
% whereiz grep
/usr/bin/grep /usr/5bin/grep

On my system, the /usr/bin directory holds a Berkeley-like version of a command. The /usr/5bin directory holds System V versions. /usr/bin is first in my path, but it's good to know if there's also a System V version. whereiz also lets you see if there are both local and system versions of the same command in your path.

Here's the script. The name ends in a z because many UNIX versions already have a whereis (50.5) command.



















&& 






#! /bin/sh

# COMMAND THAT TESTS FOR EXECUTABLE FILES... SYSTEM-DEPENDENT:
testx="test -x"

# REPLACE NULL FIELD IN $PATH WITH A .
fixpath="`echo $PATH | sed \
    -e 's/^:/.:/' \
    -e 's/::/:.:/g' \
    -e 's/:$/:./'`"

IFS=":  "       # SET $IFS (COLON, SPACE, TAB) FOR PARSING $PATH
for command
do
    where=""            # ZERO OUT $where

    # IF DIRECTORY HAS EXECUTABLE FILE, ADD IT TO LIST: 
    for direc in $fixpath
    do $testx $direc/$command && where="$where $direc/$command" 
    done

    case "$where" in
    ?*) echo $where ;;  # IF CONTAINS SOMETHING, OUTPUT IT
    esac
done

The sed (34.24) command "fixes" your PATH. It replaces a null directory name (:: in the middle of the PATH or a single : at the start or end of the PATH), which stands for the current directory. The null member is changed to the relative pathname for the current directory, a dot (1.21), so the direc shell variable in the loop won't be empty. In line 12, the double quotes ("") have colon, space, and tab characters between them. This sets the IFS (35.21) variable to split the "fixed" search path, at the colon characters, into separate directories during the for loop (44.16). That's a useful way to handle any colon-separated list.

- JP


Previous: 4.9 Setting Up vi with the .exrc File UNIX Power ToolsNext: 5. Setting Up Your Terminal
4.9 Setting Up vi with the .exrc File Book Index5. Setting Up Your Terminal

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System