@@ -53,53 +53,58 @@ WITHOUT_SUBSHELL=1
5353# ###########################################################
5454
5555function print_help() {
56- printf " Usage: %s %s\n" " ${MY_NAME} " " -p <path> -a <cmd> -d <cmd> [-t <cmd> -w <str> -i <int> -v]"
57- printf " %s %s\n" " ${MY_NAME} " " --help"
58- printf " %s %s\n" " ${MY_NAME} " " --version"
59- printf " \n"
60- printf " %s\n" " ${MY_NAME} will look for directory changes (added and deleted directories) under the specified"
61- printf " path (-p) and will execute specified commands or shell scripts (-a, -d) depending on the event.\n"
62- printf " Once all events have happened during one round (-i), a trigger command can be executed (-t).\n"
56+ printf " Usage: %s %s\\ n" " ${MY_NAME} " " -p <path> -a <cmd> -d <cmd> [-t <cmd> -w <str> -i <int> -v]"
57+ printf " %s %s\\ n" " ${MY_NAME} " " --help"
58+ printf " %s %s\\ n" " ${MY_NAME} " " --version"
59+ printf " \\ n"
60+ printf " %s\\ n" " ${MY_NAME} will look for directory changes (added and deleted directories) under the specified"
61+ printf " path (-p) and will execute specified commands or shell scripts (-a, -d) depending on the event.\\ n"
62+ printf " Once all events have happened during one round (-i), a trigger command can be executed (-t).\\ n"
6363 printf " Note, the trigger command will only be execute when at least one add or delete command has succeeded with exit code 0."
64- printf " \n"
65- printf " \nRequired arguments:\n"
66- printf " -p <path> %s\n" " Path to directoy to watch for changes."
67- printf " -a <cmd> %s\n" " Command to execute when a directory was added."
68- printf " %s\n" " You can also append the following placeholders to your command string:"
69- printf " %s\n" " %p The full path of the directory that changed (added, deleted)."
70- printf " %s\n" " %n The name of the directory that changed (added, deleted)."
71- printf " %s\n" " Example: -a \" script.sh -f %p -c %n -a %p\" "
72- printf " -d <cmd> %s\n" " Command to execute when a directory was deletd."
73- printf " %s\n" " You can also append the following placeholders to your command string:"
74- printf " %s\n" " %p The full path of the directory that changed (added, deleted)."
75- printf " %s\n" " %n The name of the directory that changed (added, deleted)."
76- printf " %s\n" " Example: -b \" script.sh -f %p -c %n -a %p\" "
77- printf " \nOptional arguments:\n"
78- printf " -t <cmd> %s\n" " Command to execute after all directories have been added or deleted during one round."
79- printf " %s\n" " No argument will be appended."
80- printf " -w <str> %s\n" " The directory watcher to use. Valid values are:"
81- printf " %s\n" " 'inotify': Uses inotifywait to watch for directory changes."
82- printf " %s\n" " 'bash': Uses a bash loop to watch for directory changes."
83- printf " %s\n" " The default is to use 'bash' as the watcher."
84- printf " -i <int> %s\n" " When using the bash watcher, specify the interval in seconds for how often"
85- printf " %s\n" " to look for directory changes."
86- printf " -v %s\n" " Verbose output."
87- printf " \nMisc arguments:\n"
88- printf " --help %s\n" " Show this help screen."
89- printf " --version %s\n" " Show version information."
64+ printf " \\ n"
65+ printf " \\ nRequired arguments:\ \ n"
66+ printf " -p <path> %s\\ n" " Path to directoy to watch for changes."
67+ printf " -a <cmd> %s\\ n" " Command to execute when a directory was added."
68+ printf " %s\\ n" " You can also append the following placeholders to your command string:"
69+ printf " %s\\ n" " %p The full path of the directory that changed (added, deleted)."
70+ printf " %s\\ n" " %n The name of the directory that changed (added, deleted)."
71+ printf " %s\\ n" " Example: -a \" script.sh -f %p -c %n -a %p\" "
72+ printf " -d <cmd> %s\\ n" " Command to execute when a directory was deletd."
73+ printf " %s\\ n" " You can also append the following placeholders to your command string:"
74+ printf " %s\\ n" " %p The full path of the directory that changed (added, deleted)."
75+ printf " %s\\ n" " %n The name of the directory that changed (added, deleted)."
76+ printf " %s\\ n" " Example: -b \" script.sh -f %p -c %n -a %p\" "
77+ printf " \\ nOptional arguments:\ \ n"
78+ printf " -t <cmd> %s\\ n" " Command to execute after all directories have been added or deleted during one round."
79+ printf " %s\\ n" " No argument will be appended."
80+ printf " -w <str> %s\\ n" " The directory watcher to use. Valid values are:"
81+ printf " %s\\ n" " 'inotify': Uses inotifywait to watch for directory changes."
82+ printf " %s\\ n" " 'bash': Uses a bash loop to watch for directory changes."
83+ printf " %s\\ n" " The default is to use 'bash' as the watcher."
84+ printf " -i <int> %s\\ n" " When using the bash watcher, specify the interval in seconds for how often"
85+ printf " %s\\ n" " to look for directory changes."
86+ printf " -v %s\\ n" " Verbose output."
87+ printf " \\ nMisc arguments:\ \ n"
88+ printf " --help %s\\ n" " Show this help screen."
89+ printf " --version %s\\ n" " Show version information."
9090}
9191
9292function print_version() {
93- printf " Name: %s\n" " ${MY_NAME} "
94- printf " Version: %s (%s)\n" " ${MY_VERSION} " " ${MY_DATE} "
95- printf " Author: %s (%s)\n" " ${MY_AUTHOR} " " ${MY_GPGKEY} "
96- printf " License: %s\n" " ${MY_LICENSE} "
97- printf " URL: %s\n" " ${MY_URL} "
93+ printf " Name: %s\\ n" " ${MY_NAME} "
94+ printf " Version: %s (%s)\\ n" " ${MY_VERSION} " " ${MY_DATE} "
95+ printf " Author: %s (%s)\\ n" " ${MY_AUTHOR} " " ${MY_GPGKEY} "
96+ printf " License: %s\\ n" " ${MY_LICENSE} "
97+ printf " URL: %s\\ n" " ${MY_URL} "
9898}
9999
100100function get_subdirs() {
101101 local path=" ${1} "
102- find " ${path} " -type d \! -name " $( basename " ${path} " ) " -prune | sort
102+ # shellcheck disable=SC2016
103+ find " ${path} " \
104+ | grep -Ev " ^${path} \$ " \
105+ | grep -Ev " ^${path} /.+/" \
106+ | xargs -n1 sh -c ' if [ -d "${1}" ]; then echo "${1}"; fi' -- \
107+ | sort
103108}
104109
105110function action() {
@@ -116,12 +121,12 @@ function action() {
116121
117122 if eval " ${action} " ; then
118123 if [ " ${verbose} " -gt " 0" ]; then
119- printf " %s: [%s] [OK] %s succeeded: %s\n" " ${MY_NAME} " " $( date ' +%Y-%m-%d %H:%M:%S' ) " " ${info} " " ${directory} "
124+ printf " %s: [%s] [OK] %s succeeded: %s\\ n" " ${MY_NAME} " " $( date ' +%Y-%m-%d %H:%M:%S' ) " " ${info} " " ${directory} "
120125 fi
121126 return 0
122127 else
123128 if [ " ${verbose} " -gt " 0" ]; then
124- printf " %s: [%s] [ERR] %s failed: %s\n" " ${MY_NAME} " " $( date ' +%Y-%m-%d %H:%M:%S' ) " " ${info} " " ${directory} "
129+ printf " %s: [%s] [ERR] %s failed: %s\\ n" " ${MY_NAME} " " $( date ' +%Y-%m-%d %H:%M:%S' ) " " ${info} " " ${directory} "
125130 fi
126131 return 1
127132 fi
@@ -137,12 +142,12 @@ function trigger() {
137142 if [ " ${changes} " -eq " 1" ]; then
138143 if eval " ${action} " ; then
139144 if [ " ${verbose} " -gt " 0" ]; then
140- printf " %s: [%s] [OK] %s succeeded: %s\n" " ${MY_NAME} " " $( date ' +%Y-%m-%d %H:%M:%S' ) " " TRIGGER" " ${action} "
145+ printf " %s: [%s] [OK] %s succeeded: %s\\ n" " ${MY_NAME} " " $( date ' +%Y-%m-%d %H:%M:%S' ) " " TRIGGER" " ${action} "
141146 fi
142147 return 0
143148 else
144149 if [ " ${verbose} " -gt " 0" ]; then
145- printf " %s: [%s] [ERR] %s failed: %s\n" " ${MY_NAME} " " $( date ' +%Y-%m-%d %H:%M:%S' ) " " TRIGGER" " ${action} "
150+ printf " %s: [%s] [ERR] %s failed: %s\\ n" " ${MY_NAME} " " $( date ' +%Y-%m-%d %H:%M:%S' ) " " TRIGGER" " ${action} "
146151 fi
147152 # Also return 0 here in order to not abort the loop
148153 return 0
255260
256261# Log startup
257262if [ " ${VERBOSE} " -gt " 0" ]; then
258- printf " %s: [%s] Starting daemon.\n" " ${MY_NAME} " " $( date ' +%Y-%m-%d %H:%M:%S' ) "
263+ printf " %s: [%s] Starting daemon.\\ n" " ${MY_NAME} " " $( date ' +%Y-%m-%d %H:%M:%S' ) "
259264fi
260265
261266
@@ -285,7 +290,7 @@ CHANGES=0
285290# Use native inotify
286291if [ " ${WATCHER} " = " inotify" ]; then
287292 if [ " ${VERBOSE} " -gt " 0" ]; then
288- printf " %s: [%s] Using native inotify to watch for changes.\n" " ${MY_NAME} " " $( date ' +%Y-%m-%d %H:%M:%S' ) "
293+ printf " %s: [%s] Using native inotify to watch for changes.\\ n" " ${MY_NAME} " " $( date ' +%Y-%m-%d %H:%M:%S' ) "
289294 fi
290295 inotifywait \
291296 --quiet \
@@ -310,7 +315,7 @@ if [ "${WATCHER}" = "inotify" ]; then
310315# Use custom inotify
311316else
312317 if [ " ${VERBOSE} " -gt " 0" ]; then
313- printf " %s: [%s] Using bash loop to watch for changes.\n" " ${MY_NAME} " " $( date ' +%Y-%m-%d %H:%M:%S' ) "
318+ printf " %s: [%s] Using bash loop to watch for changes.\\ n" " ${MY_NAME} " " $( date ' +%Y-%m-%d %H:%M:%S' ) "
314319 fi
315320 while true ; do
316321 # Get all directories
0 commit comments