-
Notifications
You must be signed in to change notification settings - Fork 6
/
quack_helper_common.sh
executable file
·131 lines (116 loc) · 3.07 KB
/
quack_helper_common.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/bin/bash
#
# Functions used by different quack scripts
#
# Input: id initialValue
# Output: lockname
function createCounter() {
local ID="$1"
local INITIAL="$2"
pushd `dirname $0` > /dev/null
local LOCKNAME="`pwd`/lock.${ID}_$$"
popd > /dev/null
local COUNTFILE="${LOCKNAME}.counter"
if [ "." == ".$INITIAL" ]; then
local INITIAL=1
fi
echo "$INITIAL" > $COUNTFILE
echo "$LOCKNAME"
}
export -f createCounter
# Input: lockname delta
# Output: Old counter from lock file + 1
function addDeltaGetCounter() {
local LOCKNAME="$1"
local DELTA="$2"
if [ "." == ".$LOCKNAME" ]; then
echo "threadedCounter: The lockname must be specified" 1>&2
exit
fi
local COUNTFILE="${LOCKNAME}.counter"
# http://stackoverflow.com/questions/8231847/bash-script-to-count-number-of-times-script-has-run
mkdir $LOCKNAME 2> /dev/null
while [[ $? -ne 0 ]] ; do
sleep 0.1
mkdir $LOCKNAME 2> /dev/null
done
local COUNTER=`cat "$COUNTFILE"`
local COUNTER=$((COUNTER+DELTA))
echo $COUNTER > "$COUNTFILE"
rm -rf $LOCKNAME
echo $COUNTER
}
export -f addDeltaGetCounter
# Input: lockname
# Output: Old counter from lock file + 1
function addGetCounter() {
addDeltaGetCounter "$1" 1
}
export -f addGetCounter
# TODO: Implement this. The problem is that bash does not support adding fractions
# and that a call to bc is costly. Maybe we can move the decimal point to
# millisecond precision and use integers instead?
# Input: lockname starttime (in nanoseconds)
# Increments the counter with milliseconds from currenttime-starttime
function updateTiming() {
local START=$2
local START=${START:0:${#START}-6}
local END=`date +%s%N`
local END=${END:0:${#END}-6}
addDeltaGetCounter $1 $((END-START))
}
export -f updateTiming
# Input: lockname
# Output: Old counter from lock file
function getCounter() {
addDeltaGetCounter "$1" 0
}
export -f getCounter
# Removed old count files
function deleteCount() {
local LOCKNAME="$1"
if [ "." == ".$LOCKNAME" ]; then
echo "deleteCount: The lockname must be specified" 1>&2
exit
fi
local COUNTFILE="${LOCKNAME}.counter"
if [ -d "$LOCKNAME" ]; then
rm -r "$LOCKNAME"
fi
if [ -f "$COUNTFILE" ]; then
rm -r "$COUNTFILE"
fi
}
export -f deleteCount
#L=`createCount foo 0`
#addGetCounter $L
#addGetCounter $L
# Skips the given number of lines and returns the rest
# If negative lines are given, the end is skipped
# Input: string lines
function skipLines() {
local TEXT="$1"
local SKIP="$2"
if [ 0 -eq $SKIP ]; then
echo ""
return
fi
if [ $SKIP -le 0 ]; then
local TAIL=true
local SKIP=$(((-1)*$SKIP))
else
local TAIL=false
fi
local LENGTH=`echo "$TEXT" | wc -l`
if [ $LENGTH -le $SKIP ]; then
echo ""
return
fi
if [ "true" == "$TAIL" ]; then
echo "$TEXT" | head -n $((LENGTH-SKIP))
else
echo "$TEXT" | tail -n $((LENGTH-SKIP))
fi
}
export -f skipLines
#skipLines "$1" "$2"