Skip to content

Commit 2e94180

Browse files
authored
Merge pull request #2 from devilbox/WAT-002
WAT-002 Customize argument injections for custom cmds
2 parents c20517a + 12f76cc commit 2e94180

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,15 @@ code 0.
3535
Required arguments:
3636
-p <path> Path to directoy to watch for changes.
3737
-a <cmd> Command to execute when a directory was added.
38-
The full path of the new dir will be appended as an argument to
39-
this command.
38+
You can also append the following placeholders to your command string:
39+
%p The full path of the directory that changed (added, deleted).
40+
%n The name of the directory that changed (added, deleted).
41+
Example: -a "script.sh -f %p -c %n -a %p"
4042
-d <cmd> Command to execute when a directory was deletd.
41-
The full path of the new dir will be appended as an argument to
42-
this command.
43+
You can also append the following placeholders to your command string:
44+
%p The full path of the directory that changed (added, deleted).
45+
%n The name of the directory that changed (added, deleted).
46+
Example: -d "script.sh -f %p -c %n -a %p"
4347

4448
Optional arguments:
4549
-t <cmd> Command to execute after all directories have been added or

watcherd

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,21 @@ function print_help() {
6565
printf "\nRequired arguments:\n"
6666
printf " -p <path> %s\n" "Path to directoy to watch for changes."
6767
printf " -a <cmd> %s\n" "Command to execute when a directory was added."
68-
printf " %s\n" "The full path of the new dir will be appended as an argument to this command."
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\""
6972
printf " -d <cmd> %s\n" "Command to execute when a directory was deletd."
70-
printf " %s\n" "The full path of the new dir will be appended as an argument to this command."
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\""
7177
printf "\nOptional arguments:\n"
7278
printf " -t <cmd> %s\n" "Command to execute after all directories have been added or deleted during one round."
7379
printf " %s\n" "No argument will be appended."
80+
printf " %s\n" "%p The full path of the directory that changed (added, deleted)."
81+
printf " %s\n" "%n The name of the directory that changed (added, deleted)."
82+
printf " %s\n" "Example: -t \"script.sh -f %p -c %n -a %p\""
7483
printf " -w <str> %s\n" "The directory watcher to use. Valid values are:"
7584
printf " %s\n" "'inotify': Uses inotifywait to watch for directory changes."
7685
printf " %s\n" "'bash': Uses a bash loop to watch for directory changes."
@@ -101,8 +110,14 @@ function action() {
101110
local action="${2}" # Add/Del command to execute
102111
local info="${3}" # Output text (ADD or DEL) for verbose mode
103112
local verbose="${4}" # Verbose?
113+
local name
114+
name="$( basename "${directory}" )"
104115

105-
if eval "${action} ${directory}"; then
116+
# Fill with placeholder values
117+
action="${action//%p/${directory}}"
118+
action="${action//%n/${name}}"
119+
120+
if eval "${action}"; then
106121
if [ "${verbose}" -gt "0" ]; then
107122
printf "[%s] [OK] %s succeeded: %s\n" "$( date '+%Y-%m-%d %H:%M:%S' )" "${info}" "${directory}"
108123
fi

0 commit comments

Comments
 (0)