aboutsummaryrefslogtreecommitdiffstats
path: root/examples/functions/whence
blob: 70b2322a997f68acd96ac24ff178e4794a993053 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#
# An almost-ksh compatible `whence' command.  This is as hairy as it is 
# because of the desire to exactly mimic ksh.
# 
# This depends somewhat on knowing the format of the output of the bash
# `builtin type' command.
#
# Chet Ramey
# chet@ins.CWRU.Edu
#
whence()
{
	local vflag= path=

	if [ "$#" = "0" ] ; then
		echo "whence: argument expected"
		return 1
	fi
	case "$1" in
		-v) vflag=1
		    shift 1
		    ;;
		-*) echo "whence: bad option: $1"
		    return 1
		    ;;
		 *) ;;
	esac

	if [ "$#" = "0" ] ; then
		echo "whence: bad argument count"
		return 1
	fi

	for cmd
	do
		if [ "$vflag" ]	 ; then
			echo $(builtin type $cmd | sed 1q)
		else
			path=$(builtin type -path $cmd)
			if [ "$path" ] ; then
				echo $path
			else
				case "$cmd" in
					/*) if [ -x "$cmd" ]; then
						echo "$cmd"
					    fi
					    ;;
					 *) case "$(builtin type -type $cmd)" in
						"") ;;
						 *) echo "$cmd"
						    ;;
					    esac
					    ;;
				esac
			fi
		fi
	done
	return 0
}