-
Notifications
You must be signed in to change notification settings - Fork 29
/
util.sh
executable file
·156 lines (147 loc) · 2.69 KB
/
util.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/bin/bash
function advance_time {
ccyymmdd=`echo $1 |cut -c1-8`
hh=`echo $1 |cut -c9-10`
mm=`echo $1 |cut -c11-12`
inc=$2
date -u -d $inc' minutes '$ccyymmdd' '$hh':'$mm +%Y%m%d%H%M
}
export -f advance_time
function diff_time {
date1=$1
date2=$2
d=0
while [[ $((date1/10000)) -ne $((date2/10000)) ]]; do
if [[ $date1 -lt $date2 ]]; then
d=$((d+1440))
date1=`advance_time $date1 1440`
else
d=$((d-1440))
date1=`advance_time $date1 -1440`
fi
done
while [[ $((date1/100)) -ne $((date2/100)) ]]; do
if [[ $date1 -lt $date2 ]]; then
d=$((d+60))
date1=`advance_time $date1 60`
else
d=$((d-60))
date1=`advance_time $date1 -60`
fi
done
while [[ $date1 -ne $date2 ]]; do
if [[ $date1 -lt $date2 ]]; then
d=$((d+1))
date1=`advance_time $date1 1`
else
d=$((d-1))
date1=`advance_time $date1 -1`
fi
done
echo $d
}
export -f diff_time
function wrf_time_string {
ccyy=`echo $1 |cut -c1-4`
mm=`echo $1 |cut -c5-6`
dd=`echo $1 |cut -c7-8`
hh=`echo $1 |cut -c9-10`
ii=`echo $1 |cut -c11-12`
echo ${ccyy}-${mm}-${dd}_${hh}:${ii}:00
}
export -f wrf_time_string
function wait_for_module {
for module in $*; do
until [ -f $module/stat ]; do sleep 10; done
until [[ `cat $module/stat` == "complete" ]]; do
sleep 15
if [[ `cat $module/stat` == "error" ]]; then
exit 1
fi
done
done
}
export -f wait_for_module
function watch_log {
logfile=$1
keyword=$2
timeout=$3
rundir=$4
l=0
t=0
until [ -s $logfile ]; do sleep 10 ; done
until [[ `tail -n5 $logfile |grep $keyword` ]]; do
sleep 1m
l1=`cat $logfile |wc -l`
if [ $l1 -eq $l ]; then
t=$((t+1))
else
l=$l1
t=0
fi
if [ $t -gt $timeout ]; then
echo "`pwd`/$logfile stagnant for $timeout minutes! Abort."
echo error > $rundir/stat
exit 1
fi
done
}
export -f watch_log
function watch_file {
filename=$1
timeout=$2
rundir=$3
t=0
until [ -f $filename ]; do
sleep 1m
t=$((t+1))
if [ $t -gt $timeout ]; then
echo Timeout waiting for $filename. Abort.
echo error > $rundir/stat
exit 1
fi
done
}
export -f watch_file
#MATH:
#greatest common divisor
function gcd {
n=$1
m=$2
while [[ $n -ne 0 ]]; do
rem=$(($m%$n))
m=$n
n=$rem
done
abs $m
}
export -f gcd
function abs {
if [ $1 -ge 0 ]; then
echo $1
else
echo $1 |cut -c2-
fi
}
export -f abs
#max/min
function max {
j=$1
for i in $@; do
if [[ $i -gt $j ]]; then
j=$i
fi
done
echo $j
}
export -f max
function min {
j=$1
for i in $@; do
if [[ $i -lt $j ]]; then
j=$i
fi
done
echo $j
}
export -f min