-
Notifications
You must be signed in to change notification settings - Fork 0
/
sleep-until-modified.sh
executable file
·58 lines (45 loc) · 1.17 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
#!/usr/bin/env bash
SCRIPTNAME=`basename "$0"`
print_help() {
cat <<EOF
EOF
Usage: $SCRIPTNAME filename
Uses 'inotifywait' to sleep until 'filename' has been modified.
Inspired by http://superuser.com/a/181543/138891
E.g. use like:
while sleep-until-modified.sh derivation.tex ; do latexmk -pdf derivation.tex ; done
EOF
}
# 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! will not work if the file contains foreign characters
sed --regexp-extended -n "/ (CLOSE_WRITE|MOVED_TO|CREATE)(,CLOSE)? ${BASENAME}\$/q" 0<&${INOTIFY[0]}