-
Notifications
You must be signed in to change notification settings - Fork 7
/
find-up
executable file
·75 lines (65 loc) · 1.58 KB
/
find-up
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
#!/usr/bin/env bash
set -euo pipefail
YP_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." >/dev/null && pwd)"
source ${YP_DIR}/sh/common.inc.sh
#- find-up 1.0
## Usage: find-up [FIND_OPTIONS] [PATHS..] [FIND_EXPRESSIONS]
## Like GNU 'find' but it searches in the parent hierarchy.
## -mindepth and -maxdepth expressions are ignored.
FIND="${FIND:-find}"
FIND_OPTIONS=()
FIND_PATHS=()
FIND_EXPRESSIONS=()
# FIND_OPTIONS
while [[ $# -gt 0 ]]; do
case "$1" in
-H|-L|-P)
FIND_OPTIONS+=("$1")
shift
;;
-O|-D)
FIND_OPTIONS+=("$1" "$2")
shift 2
;;
*)
break
;;
esac
done
# FIND_PATHS
while [[ $# -gt 0 ]]; do
case "$1" in
-*)
break
;;
*)
FIND_PATHS+=("$1")
shift
;;
esac
done
# FIND_EXPRESSIONS
while [[ $# -gt 0 ]]; do
case "$1" in
-mindepth|-maxdepth)
echo_warn "Ignoring $1 $2."
shift 2
;;
*)
FIND_EXPRESSIONS+=("$1")
shift
;;
esac
done
[[ ${#FIND_PATHS[@]} -gt 0 ]] || FIND_PATHS+=("${PWD}")
FIND_PARENT_PATHS=()
while [[ ${#FIND_PATHS[@]} -gt 0 ]]; do
FIND_PATH="${FIND_PATHS[0]}"
while true; do
FIND_PARENT_PATHS+=("${FIND_PATH}")
[[ "${FIND_PATH}" != "/" ]] || break
FIND_PATH="$(dirname "${FIND_PATH}")"
done
FIND_PATHS=("${FIND_PATHS[@]:1}")
done
exe "${FIND}" "${FIND_OPTIONS[@]}" "${FIND_PARENT_PATHS[@]}" -mindepth 1 -maxdepth 1 "${FIND_EXPRESSIONS[@]}"