blob: ba27b00d37e0599a6152eba1ba8c6d575f5d43fb [file] [log] [blame]
Jari Aalto726f6381996-08-26 18:22:31 +00001#
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#
Chet Rameyac50fba2014-02-26 09:36:43 -050011#
12# Chet Ramey <chet.ramey@case.edu>
13#
14# Copyright 1994 Chester Ramey
15#
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.
20#
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.
25#
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.
29
Jari Aalto726f6381996-08-26 18:22:31 +000030whence()
31{
32 local vflag= path=
33
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
47
48 if [ "$#" = "0" ] ; then
49 echo "whence: bad argument count"
50 return 1
51 fi
52
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
78}