-
Notifications
You must be signed in to change notification settings - Fork 584
/
075-counter.sh
executable file
·49 lines (40 loc) · 1.18 KB
/
075-counter.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
#!/bin/sh
# counter - a simple text-based page counter, with appropriate locking
myhome="/home/taylor/web/wicked/examples"
counter="$myhome/counter.dat"
lockfile="$myhome/counter.lck"
updatecounter="$myhome/updatecounter"
# Note that this script is not intended to be called directly from
# a web browser so it doesn't use the otherwise obligatory
# content-type header material.
# ascertain whether we have lockf or lockfile system apps
if [ -z $(which lockf) ] ; then
if [ -z $(which lockfile) ] ; then
echo "(counter: no locking utility available)<br>"
exit 0
else # proceed with the lockfile command
if [ ! -f $counter ] ; then
echo "0" # it'll be created shortly
else
cat $counter
fi
trap "/bin/rm -f $lockfile" 0
lockfile -1 -l 10 -s 2 $lockfile
if [ $? -ne 0 ] ; then
echo "(counter: couldn't create lockfile in time)"
exit 0
fi
$updatecounter $counter
fi
else
if [ ! -f $counter ] ; then
echo "0" # it'll be created shortly
else
cat $counter
fi
lockf -s -t 10 $lockfile $updatecounter $counter
if [ $? -ne 0 ] ; then
echo "(counter: couldn't create lockfile in time)"
fi
fi
exit 0