From 12f76cc29c8e5f10c238aba1c42a0889d2a235dc Mon Sep 17 00:00:00 2001 From: cytopia Date: Sun, 17 Sep 2017 19:16:28 +0200 Subject: [PATCH] WAT-002 Customize argument injections for custom cmds --- README.md | 12 ++++++++---- watcherd | 21 ++++++++++++++++++--- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 56a458f..6c66f3c 100644 --- a/README.md +++ b/README.md @@ -35,11 +35,15 @@ code 0. Required arguments: -p Path to directoy to watch for changes. -a Command to execute when a directory was added. - The full path of the new dir will be appended as an argument to - this command. + You can also append the following placeholders to your command string: + %p The full path of the directory that changed (added, deleted). + %n The name of the directory that changed (added, deleted). + Example: -a "script.sh -f %p -c %n -a %p" -d Command to execute when a directory was deletd. - The full path of the new dir will be appended as an argument to - this command. + You can also append the following placeholders to your command string: + %p The full path of the directory that changed (added, deleted). + %n The name of the directory that changed (added, deleted). + Example: -d "script.sh -f %p -c %n -a %p" Optional arguments: -t Command to execute after all directories have been added or diff --git a/watcherd b/watcherd index 8240758..7f8e910 100755 --- a/watcherd +++ b/watcherd @@ -65,12 +65,21 @@ function print_help() { printf "\nRequired arguments:\n" printf " -p %s\n" "Path to directoy to watch for changes." printf " -a %s\n" "Command to execute when a directory was added." - printf " %s\n" "The full path of the new dir will be appended as an argument to this command." + printf " %s\n" "You can also append the following placeholders to your command string:" + printf " %s\n" "%p The full path of the directory that changed (added, deleted)." + printf " %s\n" "%n The name of the directory that changed (added, deleted)." + printf " %s\n" "Example: -a \"script.sh -f %p -c %n -a %p\"" printf " -d %s\n" "Command to execute when a directory was deletd." - printf " %s\n" "The full path of the new dir will be appended as an argument to this command." + printf " %s\n" "You can also append the following placeholders to your command string:" + printf " %s\n" "%p The full path of the directory that changed (added, deleted)." + printf " %s\n" "%n The name of the directory that changed (added, deleted)." + printf " %s\n" "Example: -b \"script.sh -f %p -c %n -a %p\"" printf "\nOptional arguments:\n" printf " -t %s\n" "Command to execute after all directories have been added or deleted during one round." printf " %s\n" "No argument will be appended." + printf " %s\n" "%p The full path of the directory that changed (added, deleted)." + printf " %s\n" "%n The name of the directory that changed (added, deleted)." + printf " %s\n" "Example: -t \"script.sh -f %p -c %n -a %p\"" printf " -w %s\n" "The directory watcher to use. Valid values are:" printf " %s\n" "'inotify': Uses inotifywait to watch for directory changes." printf " %s\n" "'bash': Uses a bash loop to watch for directory changes." @@ -101,8 +110,14 @@ function action() { local action="${2}" # Add/Del command to execute local info="${3}" # Output text (ADD or DEL) for verbose mode local verbose="${4}" # Verbose? + local name + name="$( basename "${directory}" )" - if eval "${action} ${directory}"; then + # Fill with placeholder values + action="${action//%p/${directory}}" + action="${action//%n/${name}}" + + if eval "${action}"; then if [ "${verbose}" -gt "0" ]; then printf "[%s] [OK] %s succeeded: %s\n" "$( date '+%Y-%m-%d %H:%M:%S' )" "${info}" "${directory}" fi