-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmtimer
More file actions
executable file
·141 lines (119 loc) · 3.5 KB
/
mtimer
File metadata and controls
executable file
·141 lines (119 loc) · 3.5 KB
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
#!/bin/bash
# These MUST be a command that returns a single number in each of these:
STARTCMD="sudo fdisk -l /dev/sdb | grep Disk | head -1 | awk '{print \$5}'"
UPDATECMD="ls -l openelec_04-03-2015.img | cut -d\" \" -f5"
# Other options:
# STARTCMD="df | grep '/exports/media' | awk '{print \$3}'"
# UPDATECMD="df | grep /raid | awk '{print \$3}'"
# STARTCMD="stat -cs \$FILE"
# UPDATECMD="stat -cb \$FILE"
CALC=$(which calc)
if [ -z $CALC ] ; then
echo -n "calc not found. Would you like to install it? (Y,n)"
read INPUT
if [ $INPUT = 'n' ] || [ $INPUT = 'N' ] ; then
echo "exiting"
exit 1
else
sudo apt install apcalc
fi
fi
# change the metric to whatever is relevant.
METRIC="b"
# can specify an output file. Usually if you want it to run in the background
# and then use "watch cat outputfile" to check on progress.
# if empty, output goes to the screen
#OUTPUT="outputfile.txt"
OUTPUT=""
# all the rest should take care of itself.
output() {
if [ -z $OUTPUT ]; then
echo "$*"
else
echo "$*" >> $OUTPUT
fi
}
clearscreen() {
if [ -z $OUTPUT ] ; then
clear
else
echo "" > $OUTPUT
fi
}
humanise() {
if [ -z $1 ]; then
return 1
fi
local SECS=$1
local ETA=""
if [ $SECS -gt 604800 ]; then
local WEEKS=$($CALC -p $SECS \/ 604800 | sed 's/~//g'|cut -d. -f1)
SECS=$($CALC $SECS - \($WEEKS \* 604800\))
ETA="$WEEKS w, "
fi
if [ $SECS -gt 86400 ]; then
local DAYS=$($CALC -p $SECS \/ 86400 | sed 's/~//g'|cut -d. -f1)
SECS=$($CALC $SECS - \($DAYS \* 86400\))
if [ $DAYS -le 9 ] ; then
DAYS="00$DAYS"
fi
ETA="$ETA$DAYS d, "
elif ! [ -z "$ETA" ] ; then
ETA="${ETA}0 d, "
fi
if [ $SECS -gt 3600 ]; then
local HOURS=$($CALC -p $SECS \/ 3600 | sed 's/~//g'|cut -d. -f1)
SECS=$($CALC $SECS - \($HOURS \* 3600\))
if [ $HOURS -le 9 ] ; then
HOURS="0$HOURS"
fi
ETA="$ETA$HOURS h, "
elif ! [ -z "$ETA" ] ; then
ETA="${ETA}00 h, "
fi
if [ $SECS -gt 60 ]; then
local MINS=$($CALC -p $SECS \/ 60 | sed 's/~//g'|cut -d. -f1)
SECS=$($CALC $SECS - \($MINS \* 60\))
if [ $MINS -le 9 ] ; then
MINS="0$MINS"
fi
ETA="$ETA$MINS m, "
elif ! [ -z "$ETA" ] ; then
ETA="${ETA}00 m, "
fi
SECS=$(echo $SECS |awk '{print $1}')
if [ $SECS -le 9 ] ; then
SECS="0$SECS"
fi
ETA="$ETA$SECS s"
echo $ETA
return
}
if [ $# -eq 1 ] ; then
FILE=$1
fi
STARTTIME=$(date +%s)
TARGETSIZE=$(sh -c "$STARTCMD")
STARTSIZE=$(sh -c "$UPDATECMD")
CURRENTSIZE=0
while [ $CURRENTSIZE -lt $TARGETSIZE ]; do
sleep 2
NOW=$(date +%s)
CURRENTSIZE=$(sh -c "$UPDATECMD")
ELAPSEDTIME=$($CALC $NOW - $STARTTIME)
AMOUNTTOGO=$($CALC $TARGETSIZE - $CURRENTSIZE)
GROWTH=$($CALC $CURRENTSIZE - $STARTSIZE)
RATE=$($CALC -p $GROWTH \/ $ELAPSEDTIME | sed 's/~//g' | cut -d. -f1)
TIMETOGO=$($CALC -p $AMOUNTTOGO \/ $RATE | sed 's/~//g'|cut -d. -f1)
ESTCOMPLETIONTIME=$($CALC $NOW + $TIMETOGO | awk '{print $1}')
ETA=$(humanise $TIMETOGO)
EST=$(date --date="@$ESTCOMPLETIONTIME" '+%d %b %Y %H:%M:%S')
clearscreen
output "Target size: $TARGETSIZE $METRIC "
output "Current size: $CURRENTSIZE $METRIC"
output "Amount to go: $AMOUNTTOGO $METRIC"
output "Rate: $RATE $METRIC/s"
output ""
output "Time to go: $ETA"
output "ETA: $EST"
done