-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch_change
executable file
·105 lines (98 loc) · 2.25 KB
/
watch_change
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
98
99
100
101
102
103
104
105
#! /usr/bin/env bash
function cleanup () {
rm -f /tmp/{cur,last,diffout}.$$
exit 0
}
trap 'cleanup' SIGINT
sleeptime=1
nochangeiterations=900
timeout=1800
myargs=""
# Pop arguments off of the argument list until we hit one that isn't one of our arguments
while :
do
if [[ $1 == -h ]]
then
echo "Usage: $0 [OPTION]... COMMAND [ARGS]"
echo "Monitor output from command and show differences when they exist"
echo "Not that the command cannot currently contain any pipelines"
echo ""
echo " -i<num> Time to pause between checks."
echo " -I<num> Number of iterations to wait for no change before exiting. 0 means don't exit."
echo " -t<num> Maximum time to watch command. 0 means don't time out."
exit 0
fi
if [[ $1 == -* ]]
then
if [ ${#1} -ne 2 ]
then
# argument and param stacked together
myargs="${myargs} ${1}"
shift
elif [ ${#1} -lt 2 ]
then
break
else
myargs="${myargs} ${1} ${2}"
shift
shift
fi
else
break
fi
done
while getopts "i:I:t:" opt ${myargs}
do
case ${opt} in
i)
sleeptime=${OPTARG}
;;
I)
nochangeiterations=${OPTARG}
;;
t)
timeout=${OPTARG}
;;
esac
done
lastchange=0
if [ ${timeout} -gt 0 ]
then
deadline=$(expr $(date +%s) + ${timeout})
fi
while :
do
"$@" > /tmp/cur.$$
diff --unified /tmp/last.$$ /tmp/cur.$$ >/tmp/diffout.$$ 2>/dev/null
if [ $? -ne 0 ]
then
lastchange=0
date
if [ -s /tmp/diffout.$$ ]
then
echo "Differences:"
cat /tmp/diffout.$$
else
echo "Current contents:"
cat /tmp/cur.$$
fi
mv /tmp/cur.$$ /tmp/last.$$
else
lastchange=$(expr ${lastchange} + 1)
if [ ${nochangeiterations} -gt 0 ]
then
if [ ${lastchange} -gt ${nochangeiterations} ]
then
break
fi
fi
fi
if [ -n "${deadline}" ]
then
if [ $(date +%s) -gt ${deadline} ]
then
break
fi
fi
sleep ${sleeptime}
done