-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiando
executable file
·64 lines (58 loc) · 1.2 KB
/
piando
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
#!/bin/bash
set -u
set -e
search() {
for obj in "$1"/*; do
[[ "$obj" == *$2 ]] && [[ "$3" == "NO" ]] && [[ ! -d "$obj" ]] && printf '%s\n' "$obj"
[[ "$obj" == *$2 ]] && [[ "$3" == "YES" ]] && [[ -d "$obj" ]] && printf '%s\n' "$obj"
done
}
recursive_search() {
shopt -s globstar
for obj in "$1"/**/*; do
[[ "$obj" == *$2 ]] && [[ "$3" == "NO" ]] && [[ ! -d "$obj" ]] && printf '%s\n' "$obj"
[[ "$obj" == *$2 ]] && [[ "$3" == "YES" ]] && [[ -d "$obj" ]] && printf '%s\n' "$obj"
done
shopt -u globstar
}
RECURSIVE_SEARCH=NO
FOLDERS=NO
EXTENSION=""
DIR="."
main() {
for i in "$@"; do
case $i in
-e|--extension)
EXTENSION="$2"
shift
shift
;;
-f|--folders)
FOLDERS=YES
shift
;;
-r|--recursive)
RECURSIVE_SEARCH=YES
shift
;;
-d|--directory)
DIR="$2"
shift
shift
;;
*)
;;
esac
done
[ ! -d "$DIR" ] && die "directory doesn't exist"
if [[ "$RECURSIVE_SEARCH" == "YES" ]]; then
recursive_search "$DIR" "$EXTENSION" "$FOLDERS" | shuf
else
search "$DIR" "$EXTENSION" "$FOLDERS" | shuf
fi
}
die() {
printf 'error: %s.\n' "$1" >&2
exit 1
}
main "$@"