blob: 11613207269db448b1e6eed510d15f3a482c3cab (
plain)
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
|
human_to_size() {
awk -v human="$1" '
function trim(s) {
gsub(/^[[:space:]]+|[[:space:]]+$/, "", s)
return s
}
function parse_units(units) {
if (!units || units == "B")
return 1
if (match(units, /^.iB$/))
return 1024
if (match(units, /^.B$/))
return 1000
if (length(units) == 1)
return 1024
# parse failure: invalid base
return -1
}
function parse_scale(s) {
return index("BKMGTPE", s) - 1
}
function isnumeric(string) {
return match(string, /^[-+]?[[:digit:]]*(\.[[:digit:]]*)?/)
}
BEGIN {
# peel off the leading number as the size, fail on invalid number
human = trim(human)
if (isnumeric(human))
size = substr(human, RSTART, RLENGTH)
else
exit 1
# the trimmed remainder is assumed to be the units
units = trim(substr(human, RLENGTH + 1))
base = parse_units(units)
if (base < 0)
exit 1
scale = parse_scale(substr(units, 1, 1))
if (scale < 0)
exit 1
printf "%d\n", size * base^scale + (size + 0 > 0 ? 0.5 : -0.5)
}'
}
|