ffpath — – Search $PATH for file(s)

November 2, 2009

This is a utility script that operates similarly to the find file (ff.sh) script previously presented. But instead of searching a directory tree, it searches the components of a $PATH environment variable.

Example usage:
    $ ffpath javac jdb; # Find the Java compiler and debugger
    $ ffpath -m myscript1 myscript2; # Display the myscript1 and myscript2 scripts using more
    $ ffpath -v PERL5LIB -s’;’ -e ‘pod2text’ sybdbi.pm; # Find and convert the Sybase DBi Perl doc to text

#! /bin/sh
#  Name:        $Id: ffpath,v 1.4 2008/10/09 12:18:51 hhong Exp $
#  Synopsis:    ffpath.sh [-v envvar] [-s field_separator] [-[l1cm]] file ...
#  Description: Search $PATH for file(s)
#               Options: -v envvar  Specify environment variable
#                        -s fs      Use 'fs' for the directory field separator
#                        -l         list file info using 'ls -l' long format (default)
#                        -1         list one file per line ie: 'ls -1'
#                        -c         Use 'cat' to display the file
#                        -m         Use 'more' to display the file
#                        -e prog    Run 'prog' on file

while getopts v:s:l1cme: opt; do
        case "$opt" in
          v) envvar=$OPTARG;;
          s) sep=$OPTARG;;
          l) display='lsl';;
          1) display='ls1';;
          c) display='cat';;
          m) display='more';;
          e) display='exec'; exec=$OPTARG;;
          \?) exit 1;;
        esac
done
shift `expr $OPTIND - 1`

for file; do
        eval echo \$${envvar:-PATH} | tr {$sep:-:} '\n' | while read dir; do
                if [ -f "$dir/$file" ]; then
                        case "${display:-lsl}" in
                          ls1) ls -1 "$dir/$file";;
                          lsl) ls -l "$dir/$file";;
                          echo) echo "$dir/$file";;
                          cat) cat "$dir/$file";;
                          more) less -rXe "$dir/$file";;
                          exec) eval $exec "$dir/$file";;
                        esac
                fi
        done
done
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: