-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
which.extended.sh
125 lines (93 loc) · 3.11 KB
/
which.extended.sh
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
##################################
# shellcheck shell=sh enable=all #
##################################
which() (
if [ $# -gt 0 ]; then
while [ $# -gt 0 ]; do
case "${1}" in
--)
shift
break
;;
-i)
searchonlypath=1
shift
;;
-a)
locateall="${locateall-1}"
shift
;;
-s | -sa | -as)
locateall=
besilent=1
shift
;;
-ia | -ai)
searchonlypath=1
locateall="${locateall-1}"
shift
;;
-is | -si | -ias | -isa | -ais | -asi | -sia | -sai)
searchonlypath=1
locateall=
besilent=1
shift
;;
-*)
headline="invalid option — ${1#-}"
exitstatus=2
set --
break
;;
*)
break
;;
esac
done
fi
if [ $# -gt 0 ]; then
for command; do
located=
[ -n "${searchonlypath-}" ] || {
cmd="$(LC_ALL=C command -v -- "${command}" 2>/dev/null)"
if [ -z "${cmd}" ]; then
exitstatus=1
continue
elif [ "${cmd}" = "${command}" ] || [ ! -x "${cmd}" ]; then
[ "${cmd}" = "${command}" ] || {
cmd="${cmd#*\'}"
cmd="${cmd%\'}"
}
located=1
[ -n "${besilent-}" ] || printf "%s\n" "${cmd}"
[ -n "${locateall-}" ] || continue
fi
}
IFS=:
#############################
# shellcheck disable=SC2086 #
#############################
for path in ${PATH}; do
if [ -x "${path}/${command}" ]; then
located=1
[ -n "${besilent-}" ] || printf "%s/%s\n" "${path}" "${command}"
[ -n "${locateall-}" ] || break
fi
done
[ -n "${located}" ] || exitstatus=1
done
return "${exitstatus-0}"
fi
printf "which: %s\n\n" "${headline-locates command(s) and reports to standard output}" >&2
printf "%s\n" "USAGE" >&2
printf " %s\n" "which [-ias] command …" >&2
printf "%s\n" "OPTIONS" >&2
printf " %s %s\n" "-i" "ignore shell builtins, functions and aliases" >&2
printf " %s %s\n" "-a" "print all matches" >&2
printf " %s %s\n" "-s" "silently return 0 if all commands are located or 1 otherwise" >&2
printf "%s\n" "EXIT STATUS" >&2
printf " %s %s\n" 0 "all commands are located" >&2
printf " %s %s\n" 1 "failed to locate some command(s)" >&2
printf " %s %s\n" 2 "an invalid option is specified" >&2
return "${exitstatus-1}"
)