blob: 0ecd3dae6f2c02ede1a96ab5c8479a098bf5e83a (
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
|
#!/usr/bin/env zsh
# convert sizes in bits/bytes/kbytes/etc to human-readable strings
typeset -i sindex
suffices=( 'b' 'kb' 'mb' 'gb' 'tb' 'pb' 'eb' 'zb' 'yb' )
[[ -z $(echo $1 | grep --extended-regexp '^[0-9]*([kmgtpezy]{0,1}b){0,1}$') ]] && return 1
val=$(echo $1 | grep -o '^[0-9]*');
suf=$(echo $1 | grep -o --extended-regexp '([kmgtpezy]{0,1}b){0,1}$');
if [[ "$suf" == "" ]]; then
if [[ $(echo $val / 8 | bc) -ne 0 ]]; then
val=$(echo "scale=2; $val / 8" | bc)
else
echo $val
return 0;
fi
fi
sindex=1
for ((i=1; i <= ${#suffices}; i++)) do
[[ "$suf" == "${suffices[$i]}" ]] && let sindex=i
done
while [[ $(echo $val / 1000 | bc) -ne 0 ]]; do
val=$(echo "scale=2; $val / 1000" | bc)
let sindex=sindex+1
done
echo "${val}${suffices[$sindex]}"
|