-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_command_when_file_updates.sh
executable file
·70 lines (54 loc) · 1.58 KB
/
run_command_when_file_updates.sh
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
#!/usr/bin/env bash
# Unofficial Bash Strict Mode
# Read more about it in http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
IFS=$'\n\t'
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
set +u
source "${SCRIPT_DIR}/shml.sh"
set -u
#########
# Begin #
#########
_time_to_sleep=3
trap bashtrap EXIT
function bashtrap() {
echo
echo "$(color yellow "Stopped watching the file.")"
echo "$(color green "Thanks for using this script!")"
exit 0
}
function printUsage() {
echo "Please use this script this way:"
echo "$0 file_to_watch command(s)"
}
function check_arguments() {
if [ "$#" -lt 2 ]; then
printUsage
exit 1
fi
}
function get_last_modification_time() {
echo $(stat -c %y $@ | awk '{printf $2}')
}
check_arguments "$@"
_file_to_watch=$1
shift
_command_to_run=$*
echo "$(color green "Will watch for modifications in the $_file_to_watch file.")"
echo "$(color green "And will run the following command:")" $_command_to_run
echo "$(color lightyellow "Press CTRL + C to stop watching for modifications in the file.")"
echo "$(color yellow "If the given command exits due to a problem this script will stop.")"
_last_modification=$(get_last_modification_time $_file_to_watch)
while : ; do
sleep $_time_to_sleep
_newest_time=$(get_last_modification_time $_file_to_watch)
if [ $_newest_time != $_last_modification ]; then
echo ""
echo "$(color cyan "File $_file_to_watch was updated...")"
echo "$(color lightcyan "Running")" \"$_command_to_run\"
echo ""
$_command_to_run
_last_modification=$_newest_time
fi
done