-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch-directory
executable file
·97 lines (68 loc) · 2.09 KB
/
watch-directory
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/bin/bash
# Bash Utility to watch any directory for changes and runs a supplied custom command on the event
scriptAbsolutePath=;
helpText=;
directory=;
commandEntry=;
function displayHowToUse() {
helpText=$(cat << EOF
watch-directory
Version: 1.0.0
Usage:
watch-directory [OPTIONS]
Options:
<-d | --directory directoryName>
<-c | --command commandEntry>
<-h | --help>
Synopsis:
Purpose: To watch a directory for changes and also runs a command whenever a change is observed
Options:
-d | --directory: This option receives the name of the directory to watch.
-c | --command: This option receives the name of the command to run whenever a change happens in the directory.
-h | --help: This option displays this help page.
Author: Daniel Okwufulueze [https://github.com/DOkwufulueze]
Date: 02/05/2018
EOF
);
}
function getAbsolutePathOfSelf() {
pushd $(dirname "$0") > /dev/null;
scriptAbsolutePath="$(pwd -P)";
popd > /dev/null;
}
function beginWatch() {
fswatch -0 "${directory}" | while read -d "" event
do
[[ -f "${event}" ]] && $(${commandEntry} -f ${event});
# [[ -f "${event}" ]] && echo "${commandEntry} -f ${event}";
done
}
if [[ $# -gt 0 ]]; then
while [[ "$1" != "" ]]; do
case "$1" in
-d | --directory )
shift;
directory="$1";
;;
-c | --command )
shift;
commandEntry="$*";
;;
-h | --help )
displayHowToUse;
echo "${helpText}" | less;
exit;
;;
* )
echo "Invalid entry in watch-directory invocation.";
exit;
;;
esac
[[ -n "${commandEntry}" && -n "${directory}" ]] && break;
shift;
done
fi
getAbsolutePathOfSelf;
firstDirectoryCharacter=$(echo "${directory}" | head -c 1);
[[ -n "${firstDirectoryCharacter}" && "${firstDirectoryCharacter}" != "/" ]] && directory="${scriptAbsolutePath}/${directory}";
beginWatch;