-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrerun
executable file
·63 lines (53 loc) · 1.19 KB
/
rerun
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
#!/usr/bin/env bash
usage() {
printf '%s\n' "Usage: rerun [-h] CMD PATTERN
rerun detects close_write events in the current directory for files matching
the supplied PATTERN and executes the specified shell CMD whenever one is
detected.
where:
-h, --help - show this help text
CMD - the shell command to run
PATTERN - regex identifying files to watch"
}
main() {
inotifywait -e close_write -m . |
while read -r directory events filename; do
if [[ "${filename}" =~ ${PATTERN} ]] ; then
echo "${events}: ${filename}"
bash -c "${CMD}"
fi
done
}
# Option parsing
declare PARAMS=""
declare CMD
declare PATTERN
while (( "$#" )); do
case $1 in
-h|--help) # display help message
usage
exit 1
;;
--) # End argument parsing
shift
break
;;
-*|--*) # unsupported flags
echo "Unsupported flag: $1" >&2
usage
exit 1
;;
*) # preserve positional arguments
PARAMS="${PARAMS} $1"
shift
;;
esac
done
# set positional arguments in their proper place
eval set -- "${PARAMS}"
# parse positional args
CMD="$1"
PATTERN="$2"
# Freeze configuration flags
readonly CMD PATTERN
main