init version.
[bush.git] / examples / functions / whence
blob97268b61f00a1dc458b2a544a6f7e47a830caf4f
2 # An almost-ksh compatible `whence' command.  This is as hairy as it is 
3 # because of the desire to exactly mimic ksh.
4
5 # This depends somewhat on knowing the format of the output of the bush
6 # `builtin type' command.
8 # Chet Ramey
9 # chet@ins.CWRU.Edu
12 #  Chet Ramey <chet.ramey@case.edu>
14 #  Copyright 1994 Chester Ramey
16 #   This program is free software; you can redistribute it and/or modify
17 #   it under the terms of the GNU General Public License as published by
18 #   the Free Software Foundation; either version 2, or (at your option)
19 #   any later version.
21 #   TThis program is distributed in the hope that it will be useful,
22 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
23 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 #   GNU General Public License for more details.
26 #   You should have received a copy of the GNU General Public License
27 #   along with this program; if not, write to the Free Software Foundation,
28 #   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 whence()
32         local vflag= path=
34         if [ "$#" = "0" ] ; then
35                 echo "whence: argument expected"
36                 return 1
37         fi
38         case "$1" in
39                 -v) vflag=1
40                     shift 1
41                     ;;
42                 -*) echo "whence: bad option: $1"
43                     return 1
44                     ;;
45                  *) ;;
46         esac
48         if [ "$#" = "0" ] ; then
49                 echo "whence: bad argument count"
50                 return 1
51         fi
53         for cmd
54         do
55                 if [ "$vflag" ]  ; then
56                         echo $(builtin type $cmd | sed 1q)
57                 else
58                         path=$(builtin type -path $cmd)
59                         if [ "$path" ] ; then
60                                 echo $path
61                         else
62                                 case "$cmd" in
63                                         /*) if [ -x "$cmd" ]; then
64                                                 echo "$cmd"
65                                             fi
66                                             ;;
67                                          *) case "$(builtin type -type $cmd)" in
68                                                 "") ;;
69                                                  *) echo "$cmd"
70                                                     ;;
71                                             esac
72                                             ;;
73                                 esac
74                         fi
75                 fi
76         done
77         return 0