-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
which.classic.sh
82 lines (62 loc) · 1.97 KB
/
which.classic.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
##################################
# shellcheck shell=sh enable=all #
##################################
which() (
if [ $# -gt 0 ]; then
while [ $# -gt 0 ]; do
case "${1}" in
--)
shift
break
;;
-a)
locateall="${locateall-1}"
shift
;;
-s | -sa | -as)
locateall=
besilent=1
shift
;;
-*)
headline="invalid option — ${1#-}"
exitstatus=2
set --
break
;;
*)
break
;;
esac
done
fi
if [ $# -gt 0 ]; then
for command; do
located=
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 [-as] command …" >&2
printf "%s\n" "OPTIONS" >&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}"
)