-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindpath
executable file
·90 lines (74 loc) · 1.46 KB
/
findpath
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
#!/bin/bash
script="$(basename "$0")"
searchFuncsAndAliases=false
searchAll=false
usage() {
cat <<-DOC
Usage: $script [-fa] <regex>
Check if a binary exists in your PATH. The default is binaries only (including duplicates).
Options:
-f, --funcs search only functions + aliases
-a, --all search all (binaries, funcs, aliases)
DOC
}
binaries() {
IFS=: read -ra pathEntries <<< "$PATH"
for pathEntry in "${pathEntries[@]}"; do
if [[ -d $pathEntry ]]; then
binaries=$(find "$pathEntry" -type f -executable -printf '%f\n' 2> /dev/null | grep -P "$regex" | sort)
if [[ -n $binaries ]]; then
printf "\e[38;5;9mBinaries in %s:\e[0m\n" "$pathEntry"
echo "$binaries"
fi
fi
done
}
funcs() {
mkfifo pipe
headerPrinted=false
while IFS= read -r line; do
if ! $headerPrinted; then
printf "\e[38;5;9mFunctions and aliases:\e[0m\n"
headerPrinted=true
fi
echo "$line"
done < pipe &
readOutputPID=$!
findfunc "$regex" > pipe
exitCode=$?
wait $readOutputPID
rm pipe
if [[ $exitCode = 1 ]]; then
echo -e "\nNo matching functions or aliases found."
fi
}
if [[ $# -eq 0 || $1 =~ -[fa] && $# -eq 1 ]]; then
usage
exit 1
fi
while [ $# -gt 0 ]; do
case $1 in
-f | --funcs)
searchFuncsAndAliases=true
;;
-a | --all)
searchAll=true
;;
-h | --help)
usage
exit
;;
*)
regex=$1
;;
esac
shift
done
if $searchFuncsAndAliases; then
funcs
elif $searchAll; then
binaries
funcs
else
binaries
fi