It is currently Fri, 15 Oct 2021 21:33:20 GMT



 
Author Message
 summary on which(1) in Bourne shell
I asked whether there is any substitute for which(1) that
works in Bourne shell scripts.

Mike Khaw <k...@parcplace.com> pointed out the Bourne shell builtin
"type" which does about the same thing as /usr/ucb/which and is much
faster.

And Byron Rakitzis <by...@archone.tamu.edu> contributed a script
which I modified as follows.

#!/bin/sh
spacepath=`echo $PATH | sed 's/:/ /g'`
for i in $spacepath; do
        if test -x $i/$1; then
                echo $i/$1
                exit 0
        fi
done
echo $1 not found 1>&2
exit 1

This can be modified to find man pages in $MANPATH, etc.

Sorry for cluttering up the newsgroup with a very elementary question,
but the answer wasn't obvious to me at the time.



 Sat, 26 Feb 1994 11:28:08 GMT   
 summary on which(1) in Bourne shell
In article <7-cHnw...@cs.psu.edu> simp...@math.psu.edu (Stephen G Simpson) writes:

Why use sed? you can achieve the same thing with shell builtins:

        spacepath=`IFS=:; echo $PATH`

--
Chris Bertin    |   Unisys NCG   |      chr...@Convergent.COM
                | (408) 435-3762 | [...!uunet!pyramid!ctnews!chrisb]



 Sun, 27 Feb 1994 01:01:40 GMT   
 summary on which(1) in Bourne shell

        #!/bin/sh
        spacepath=`echo $PATH | sed 's/:/ /g'`
        for i in $spacepath; do
                if test -x $i/$1; then
                        echo $i/$1
                        exit 0
                fi
        done
        echo $1 not found 1>&2
        exit 1

        spacepath=`IFS=:; echo $PATH`

Why use sh? You can achieve the same thing in rc without the twisted $IFS semantics:

        for (i in $path) {
                if (test -x $i/$1) {
                        echo $i/$1
                        exit 0
                }
        }
        echo $1 not found >[1=2]
        exit 1
--
Byron Rakitzis
by...@archone.tamu.edu



 Sun, 27 Feb 1994 22:10:38 GMT   
 summary on which(1) in Bourne shell

This script does not work properly.  If you have the current directory
defined as an element of your path as with ::, then the above will not
find the executable in the current directory.

While use a subshell?

The following is what I use.  It is slightly different from which
in the following ways:

1) Much faster.  Faster than the above too.
2) Does not deal with aliases or functions.  (Note: the csh built-in deals
   with aliases in your .cshrc file, not with the current state of your
   currently running csh).  I presume you know what your aliases are.
3) Provides a list of all the files with $1 as their name.  This feature
   is useful because it let's you see alternatives to the file you are
   executing.  For example, some systems have more than one grep or cc.
4) A minor modification would provide only the first name in the path.
5) Another minor modification could allow multiple arguments to be handled.
6) If you have the ksh, the built-in whence is very useful and fast.

case $# in
        1)      IFS=:
                for i in $PATH; do
                        case $i in
                                " "|"")     ls="$ls:$1" ;;
                                *)      ls="$ls:$i/$1"
                        esac
                done
                # put a junk name at the beginning.
                ls="/sdlfk/sdlfke/sdweoir$ls"
                /bin/ls $ls  2> /dev/null
                ;;
        0) echo fw: no files given 1>&2 ;;
        *) echo fw: one file only 1>&2
esac
exit

Bug report -- the for mechanism puts a : at the beginning of the
ls string.  Thus, there is an implicit null string at the beginning,
and hence the entire current directory gets ls'ed.  We should either
change the mechanism or delete the initial : (with the ksh built-in),
but since the ksh is not universally available including here, we stick
some junk at the beginning -- it's a complete hack and distasteful.

David Gast



 Mon, 07 Mar 1994 08:28:01 GMT   
 summary on which(1) in Bourne shell
[... previous stuff deleted ...]

Actually, your script is broken. It doesn't check that the component is
an executable file. If I do 'fw X11', for example, I get a full listing
of the /usr/bin/X11 directory!!! And if you really want to make it fast,
you should use builtins only, I think.

This is my 'findpath' function (for ksh only, but a minor mod involving
a case statement at the beginning would make it work for sh). It handle
these cases and it also handle matching patterns (like findpath '*cc*').
And it uses only builtins.

#
# find commands in path (matching patterns OK)
#
function findpath
{
        : ${@:?"Usage: $0 command ..."}
        [ "${PATH##:}" = "$PATH" ] && _path="" || _path="."
        _path="$_path `eval IFS=:; echo $PATH`"
        set -f
        for _arg do
                set +f
                for _pathel in $_path; do
                        for _ff in $_pathel/$_arg; do
                                [ -f $_ff -a -x $_ff ] && echo $_ff
                        done
                done
        done
        unset _arg _path _pathel _ff

--
Chris Bertin    |   Unisys NCG   |      chr...@Convergent.COM
                | (408) 435-3762 | [...!uunet!pyramid!ctnews!chrisb]



 Wed, 09 Mar 1994 07:14:35 GMT   
 summary on which(1) in Bourne shell

   For bash, I use the following as a replacement for which(1).
Note: when I use which, I'm really interested in path names.  If I wanted
to know about builtins and such I could just use "type foobar"

which ()
{
 local paths="$(builtin type -all -path $*)"

    if [ "${paths}" ]; then
       set ${paths}
       echo $1
    fi

I don't know if any ksh implementations have the "type" builtin.

---
Noah Friedman
fried...@ai.mit.edu



 Fri, 11 Mar 1994 19:42:40 GMT   
 
   [ 6 post ] 

Similar Threads

1. Light Speed Bourne Shell! (was: Bourne shell tricks)

2. Bourne Shell compatible shells (was: Request: which shells)

3. getting environment variables from c shell using Bourne shell

4. Changing from the Bourne shell to C-shell

5. Changing from the Bourne shell to C-shell

6. Bourne Shell vs C-Shell

7. Process history under C-shell and Bourne shell

8. Bourne shell script to determine users shell

9. Bourne shell script affecting current shell with arguments


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software