-
Notifications
You must be signed in to change notification settings - Fork 6
/
bytes2readable.sh
executable file
·62 lines (44 loc) · 1.15 KB
/
bytes2readable.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
#!/bin/zsh -f
# Purpose: Given a size, in bytes, output a readable size
if [ -e "$HOME/.path" ]
then
source "$HOME/.path"
else
PATH=/usr/local/scripts:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin
fi
# assume 1 MB = 1000 bytes, unless told otherwise
MBYTES=1000
case "$1" in
--1024|--old)
MBYTES=1024
shift
;;
--*)
MBYTES=1000
shift
;;
esac
PRECISION="scale=2" # change this numeric value to increase decimal precision
if [ "$MBYTES" = "1000" ]
then
METRIC=('KB' 'MB' 'GB' 'TB' 'XB' 'PB') # Array of suffixes
else
METRIC=('KiB' 'MiB' 'GiB' 'TiB' 'XiB' 'PiB') # Array of suffixes
fi
for BYTES in "$@"
do
# (re)initialize MAGNITUDE variable
MAGNITUDE=0 # magnitude of 2^10
UNITS=$(echo "$BYTES" | tr -dc '[0-9]') # numeric arg val (in bytes) to be converted
if [[ "$UNITS" != "" ]]
then
while [ ${UNITS/.*} -ge $MBYTES ] # compares integers (b/c no floats in bash)
do
UNITS=$(echo "$PRECISION; $UNITS/$MBYTES" | bc) # floating point math via `bc`
((MAGNITUDE++)) # increments counter for array pointer
done
echo "$UNITS ${METRIC[$MAGNITUDE]}"
fi
done
exit 0
#EOF