@@ -25,11 +25,11 @@ IFS=$'\n'
25
25
26
26
# Versioning
27
27
MY_NAME=" watcherd"
28
- MY_DATE=" 2022-12-18 "
28
+ MY_DATE=" 2023-03-01 "
29
29
MY_URL=" https://github.com/devilbox/watcherd"
30
30
MY_AUTHOR=
" cytopia <[email protected] >"
31
31
MY_GPGKEY=" 0xA02C56F0"
32
- MY_VERSION=" 1.0.7 "
32
+ MY_VERSION=" 1.1.0 "
33
33
MY_LICENSE=" MIT"
34
34
35
35
# Default settings
@@ -38,6 +38,7 @@ INTERVAL=1
38
38
VERBOSE=0
39
39
WATCHER=" bash"
40
40
WATCH_DIR=
41
+ EXCLUDE=
41
42
CMD_ADD=
42
43
CMD_DEL=
43
44
CMD_TRIGGER=
@@ -107,8 +108,10 @@ function print_help() {
107
108
printf " %s\\ n" " You can also append the following placeholders to your command string:"
108
109
printf " %s\\ n" " %p The full path of the directory that changed (added, deleted)."
109
110
printf " %s\\ n" " %n The name of the directory that changed (added, deleted)."
110
- printf " %s\\ n" " Example: -b \" script.sh -f %p -c %n -a %p\" "
111
+ printf " %s\\ n" " Example: -d \" script.sh -f %p -c %n -a %p\" "
111
112
printf " \\ nOptional arguments:\\ n"
113
+ printf " -e <regex> %s\\ n" " Exclude regex for directories to ignore."
114
+ printf " %s\\ n" " E.g.: -e '\\ .*' to ignore dot directories."
112
115
printf " -t <cmd> %s\\ n" " Command to execute after all directories have been added or deleted during one round."
113
116
printf " %s\\ n" " No argument will be appended."
114
117
printf " -w <str> %s\\ n" " The directory watcher to use. Valid values are:"
@@ -141,15 +144,25 @@ function get_subdirs() {
141
144
# | grep -Ev "^${path}/.+/" \
142
145
# | sort
143
146
path=" ${path%/ } /"
144
- (ls -1 -a " ${path} " || true) \
147
+ # shellcheck disable=SC2012
148
+ cd " ${path} " && ls -1 -a . \
145
149
| tr ' \r\n' ' \000' \
146
150
| tr ' \n' ' \000' \
147
151
| tr ' \r' ' \000' \
148
152
| xargs \
149
153
-0 \
150
154
-P" $( getconf _NPROCESSORS_ONLN) " \
151
155
-n1 \
152
- sh -c " if [ -d \" ${path} \$ {1}\" ] && [ \"\$ {1}\" != \" .\" ] && [ \"\$ {1}\" != \" ..\" ]; then echo \" ${path} \$ {1}\" ; fi" -- \
156
+ sh -c "
157
+ if [ -d \"\$ {1}\" ] && [ \"\$ {1}\" != \" .\" ] && [ \"\$ {1}\" != \" ..\" ]; then
158
+ if [ -n \" ${EXCLUDE} \" ]; then
159
+ if ! echo \"\$ {1}\" | grep -E '${EXCLUDE} ' >/dev/null; then
160
+ echo \" ${path} \$ {1}\" ;
161
+ fi
162
+ else
163
+ echo \" ${path} \$ {1}\" ;
164
+ fi;
165
+ fi" -- \
153
166
| sort
154
167
}
155
168
@@ -241,6 +254,14 @@ while [ $# -gt 0 ]; do
241
254
fi
242
255
CMD_DEL=" ${1} "
243
256
;;
257
+ -e)
258
+ shift
259
+ if [ -z " ${1:- } " ]; then
260
+ >&2 echo " ${MY_NAME} : -e requires an argument."
261
+ exit 1
262
+ fi
263
+ EXCLUDE=" ${1} "
264
+ ;;
244
265
-t)
245
266
shift
246
267
if [ -z " ${1:- } " ]; then
@@ -354,26 +375,48 @@ CHANGES=0
354
375
# Use native inotify
355
376
if [ " ${WATCHER} " = " inotify" ]; then
356
377
log " info" " Using native inotify to watch for changes."
357
- inotifywait \
358
- --quiet \
359
- --monitor \
360
- --event create \
361
- --event modify \
362
- --event delete \
363
- --event move \
364
- --format ' %e/\\%w%f' \
365
- " ${WATCH_DIR} " | while read -r output; do
366
- d=" ${output##* \\ } "
367
- if [[ " ${output} " =~ ^(CREATE| MOVED_TO),ISDIR/\\ ]]; then
368
- if action " ${d} " " ${CMD_ADD} " " ADD" " ${VERBOSE} " ; then
369
- trigger " ${CMD_TRIGGER} " " 1" " ${VERBOSE} "
378
+ if [ -n " ${EXCLUDE} " ]; then
379
+ inotifywait \
380
+ --monitor \
381
+ --exclude " ${EXCLUDE} " \
382
+ --event create \
383
+ --event modify \
384
+ --event delete \
385
+ --event move \
386
+ --format ' %e/\\%w%f' \
387
+ " ${WATCH_DIR} " | while read -r output; do
388
+ d=" ${output##* \\ } "
389
+ if [[ " ${output} " =~ ^(CREATE| MOVED_TO),ISDIR/\\ ]]; then
390
+ if action " ${d} " " ${CMD_ADD} " " ADD" " ${VERBOSE} " ; then
391
+ trigger " ${CMD_TRIGGER} " " 1" " ${VERBOSE} "
392
+ fi
393
+ elif [[ " ${output} " =~ ^(DELETE| MOVED_FROM),ISDIR/\\ ]]; then
394
+ if action " ${d} " " ${CMD_DEL} " " DEL" " ${VERBOSE} " ; then
395
+ trigger " ${CMD_TRIGGER} " " 1" " ${VERBOSE} "
396
+ fi
370
397
fi
371
- elif [[ " ${output} " =~ ^(DELETE| MOVED_FROM),ISDIR/\\ ]]; then
372
- if action " ${d} " " ${CMD_DEL} " " DEL" " ${VERBOSE} " ; then
373
- trigger " ${CMD_TRIGGER} " " 1" " ${VERBOSE} "
398
+ done
399
+ else
400
+ inotifywait \
401
+ --monitor \
402
+ --event create \
403
+ --event modify \
404
+ --event delete \
405
+ --event move \
406
+ --format ' %e/\\%w%f' \
407
+ " ${WATCH_DIR} " | while read -r output; do
408
+ d=" ${output##* \\ } "
409
+ if [[ " ${output} " =~ ^(CREATE| MOVED_TO),ISDIR/\\ ]]; then
410
+ if action " ${d} " " ${CMD_ADD} " " ADD" " ${VERBOSE} " ; then
411
+ trigger " ${CMD_TRIGGER} " " 1" " ${VERBOSE} "
412
+ fi
413
+ elif [[ " ${output} " =~ ^(DELETE| MOVED_FROM),ISDIR/\\ ]]; then
414
+ if action " ${d} " " ${CMD_DEL} " " DEL" " ${VERBOSE} " ; then
415
+ trigger " ${CMD_TRIGGER} " " 1" " ${VERBOSE} "
416
+ fi
374
417
fi
375
- fi
376
- done
418
+ done
419
+ fi
377
420
# Use custom inotify
378
421
else
379
422
log " info" " Using bash loop to watch for changes."
0 commit comments