70b2322a997f68acd96ac24ff178e4794a993053
[platform/upstream/bash.git] / examples / functions / whence
1 #
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 bash
6 # `builtin type' command.
7 #
8 # Chet Ramey
9 # chet@ins.CWRU.Edu
10 #
11 whence()
12 {
13         local vflag= path=
14
15         if [ "$#" = "0" ] ; then
16                 echo "whence: argument expected"
17                 return 1
18         fi
19         case "$1" in
20                 -v) vflag=1
21                     shift 1
22                     ;;
23                 -*) echo "whence: bad option: $1"
24                     return 1
25                     ;;
26                  *) ;;
27         esac
28
29         if [ "$#" = "0" ] ; then
30                 echo "whence: bad argument count"
31                 return 1
32         fi
33
34         for cmd
35         do
36                 if [ "$vflag" ]  ; then
37                         echo $(builtin type $cmd | sed 1q)
38                 else
39                         path=$(builtin type -path $cmd)
40                         if [ "$path" ] ; then
41                                 echo $path
42                         else
43                                 case "$cmd" in
44                                         /*) if [ -x "$cmd" ]; then
45                                                 echo "$cmd"
46                                             fi
47                                             ;;
48                                          *) case "$(builtin type -type $cmd)" in
49                                                 "") ;;
50                                                  *) echo "$cmd"
51                                                     ;;
52                                             esac
53                                             ;;
54                                 esac
55                         fi
56                 fi
57         done
58         return 0
59 }