-
Notifications
You must be signed in to change notification settings - Fork 10
/
sleep_until_modified.sh
executable file
·61 lines (50 loc) · 1.34 KB
/
sleep_until_modified.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
#!/bin/bash
#
# Please consider using a better tool instead of this crappy script: https://github.com/eradman/entr
SCRIPTNAME=`basename "$0"`
print_help() {
cat << EOF
Usage: $SCRIPTNAME filename
Uses 'inotifywait' to sleep until 'filename' has been modified.
Inspired by http://superuser.com/questions/181517/how-to-execute-a-command-whenever-a-file-changes/181543#181543
TODO: rewrite this as a simple Python script, using pyinotify
EOF
}
# check dependencies
if ! type inotifywait &>/dev/null ; then
echo "You are missing the inotifywait dependency. Install the package inotify-tools (apt-get install inotify-tools)"
exit 1
fi
# parse_parameters:
while [[ "$1" == -* ]] ; do
case "$1" in
-h|-help|--help)
print_help
exit
;;
--)
#echo "-- found"
shift
break
;;
*)
echo "Invalid parameter: '$1'"
exit 1
;;
esac
done
if [ "$#" != 1 ] ; then
echo "Incorrect parameters. Use --help for usage instructions."
exit 1
fi
FULLNAME="$1"
BASENAME=`basename "$FULLNAME"`
DIRNAME=`dirname "$FULLNAME"`
coproc INOTIFY {
inotifywait -q -m -e close_write,moved_to,create ${DIRNAME} &
trap "kill $!" 1 2 3 6 15
wait
}
trap "kill $INOTIFY_PID" 0 1 2 3 6 15
# BUG! Não vai funcionar com arquivos contendo caracteres estranhos
sed --regexp-extended -n "/ (CLOSE_WRITE|MOVED_TO|CREATE)(,CLOSE)? ${BASENAME}\$/q" 0<&${INOTIFY[0]}