which program and modification

which program and modification

Post by joyc » Mon, 16 Jun 2003 14:23:17



hi guys,

i have a challange for you. i spend my last 2 days to figure out how to
modify the which code so that it knows about the shell built ins such as
exit etc. i am really down regarding this because i have spend so much time
on it and no outcome. just error messages. maybe you have any idea.

that is the source code:

# which cmd: which cmd in PATH is executed, version 1

opath=$PATH

PATH=/bin:/usr/bin

case "$1" in

    -a) match='ls $1';;

    *) match='exit 0'

esac

case "$2" in

        0) echo 'Useage: which command' 1>&2; exit 2

esac

            for i in `echo $PATH | sed 's/^:/.:/

                                                        s/::/:.:/g

                                                            s/:$/:./

                                                                s/:/ /g'`

            do

                    if test -x $i/$2 # use test -x if you can

            then

            if

                then

                    "$1"=-a

                echo $i/$2

            `$match` # found it

        fi

    fi

        done

        exit 1 # not found

 
 
 

which program and modification

Post by joyc » Mon, 16 Jun 2003 14:25:45


me again, would that work??

#which cmd: which command in PATH is executed, final version

opath=$PATH

PATH=/bin:/usr/bin

case $# in

0) echo 'Usage: which command' 1>&2; exit 2

-a) match='ls $#3333

esac

for i in `echo $opath | sed 's/^:/.:/

s/::/:.:/g

s/:$/:./

s/:/ /g'`

do

if test -x $i/$1 # this is /bin/test

then # or /usr/bin/test only

echo $i/$1

exit 0 # found it

fi

done

exit 1 #not found


Quote:> hi guys,

> i have a challange for you. i spend my last 2 days to figure out how to
> modify the which code so that it knows about the shell built ins such as
> exit etc. i am really down regarding this because i have spend so much
time
> on it and no outcome. just error messages. maybe you have any idea.

> that is the source code:

> # which cmd: which cmd in PATH is executed, version 1

> opath=$PATH

> PATH=/bin:/usr/bin

> case "$1" in

>     -a) match='ls $1';;

>     *) match='exit 0'

> esac

> case "$2" in

>         0) echo 'Useage: which command' 1>&2; exit 2

> esac

>             for i in `echo $PATH | sed 's/^:/.:/

>                                                         s/::/:.:/g

>                                                             s/:$/:./

>                                                                 s/:/ /g'`

>             do

>                     if test -x $i/$2 # use test -x if you can

>             then

>             if

>                 then

>                     "$1"=-a

>                 echo $i/$2

>             `$match` # found it

>         fi

>     fi

>         done

>         exit 1 # not found


 
 
 

which program and modification

Post by Pascal Bourguigno » Tue, 17 Jun 2003 03:52:44



> hi guys,

> i have a challange for you. i spend my last 2 days to figure out how to

You mean that's a challenge for YOU.

Quote:> modify the which code so that it knows about the shell built ins such as
> exit etc. i am really down regarding this because i have spend so much time
> on it and no outcome. just error messages. maybe you have any idea.

Just write:

    alias which=type

$ which ls
ls is aliased to `LC_COLLATE="" /bin/ls -FaCN'
$ which test
test is a shell builtin
$ which cat
cat is hashed (/bin/cat)

Quote:> that is the source code:

You  need  to  learn  more  about script  writting.   There  are  good
tutorials on the web.  At least, read your shell manual:

    man bash

--
__Pascal_Bourguignon__                   http://www.informatimago.com/
----------------------------------------------------------------------
Do not adjust your mind, there is a fault in reality.

 
 
 

which program and modification

Post by David Thompso » Sun, 22 Jun 2003 03:37:14



Quote:> i have a challange for you. i spend my last 2 days to figure out how to
> modify the which code so that it knows about the shell built ins such as
> exit etc. i am really down regarding this because i have spend so much
time
> on it and no outcome. just error messages. maybe you have any idea.

Arrg, much torture!  Your code, hard to read, poorly
formatted, my eyes, help!  ;)

I recently updated my 'witch' shell script to ksh93, thought
you might like to look at it.

--begin cut--
#!/usr/local/bin/ksh
#--------------------------------------------------------------------

#--------------------------------------------------------------------
# $Id: witch,v 1.18 2003/06/01 19:20:24 davidt Exp $

## -- CREDITS
## -- David Thompson, Dec 1991

# witch locates all occurrences of file on the search PATH (with
# execute permission) and reports the location of each occurrence
# found.  It's very similar to which, except witch prints all
# occurrences.

## -- common variables
SCRIPT="${0##*/}"
USAGE="Name
    $SCRIPT - locate all occurrences of a command
Usage
    $SCRIPT [options] command...
Where
    command      Find all occurrences of command on PATH
Options
    -nopwd       Ignore current directory
    -XXX         Pass all -XXX options to ls
    -version     Print version control information
    -h/elp       Print this help message
Examples
    witch -l perl
    witch --color perl
"

## -- timestamp when this file last updated
LAST_REVISION='$Id: witch,v 1.18 2003/06/01 19:20:24 davidt Exp $'

#--------------------------------------------------------------------
# Process command line arguments
#--------------------------------------------------------------------

OPTS=
NOPWD=false

## -- loop to process arguments
while [[ $# -ne 0 ]] ; do
    case "$1" in
        -h|-help) print "$USAGE" ; exit 0 ;;
        -version) print "$LAST_REVISION" ; exit 0 ;;
        -nopwd) NOPWD=true ;;
        --) shift ; break ;;
        -*) OPTS="$OPTS $1" ;;
        *)  break ;;
    esac
    shift
done

## -- check for missing arguments
[[ $# -eq 0 ]] && { print "$USAGE" ; exit 0 ; }

#--------------------------------------------------------------------
# MAIN LOGIC
#--------------------------------------------------------------------


    [[ $# -gt 1 ]] && print "$file:"
    for dir in ${PATH//:/ } ; do
        [[ "$dir" == "." ]] && { $NOPWD && continue ; }
        [[ -f "$dir/$file" ]] || continue
        [[ -x "$dir/$file" ]] || continue
        ls $OPTS "$dir/$file"
    done
done

exit 0
--end cut--

--
David Thompson
Foster City, CA