|
12345678910111213141516171819202122232425262728293031323334353637 |
- #!/bin/bash
- # Format size in bytes into a readable string of the form "1.23 KiB"; reads from stdin and arguments and can take multiple numbers separated by any amount of whitespace including newlines
- {
- if [ ! -t 0 ]; then cat; fi
- echo "$@"
- } |
- tr '\t ' '\n' |
- awk '
- BEGIN {
- units[0] = "B";
- units[1] = "KiB";
- units[2] = "MiB";
- units[3] = "GiB";
- units[4] = "TiB";
- units[5] = "PiB";
- }
-
- {
- size += $1;
- }
-
- END {
- if (size > 0) {
- magnitude = int(log(size) / log(1024));
- if (magnitude > 5) {
- magnitude = 5;
- }
- } else {
- magnitude = 0;
- }
- if (magnitude > 0) {
- sizeformat = "%.2f";
- } else {
- sizeformat = "%d";
- }
- printf sizeformat " %s\n", size / (1024 ^ magnitude), units[magnitude];
- }'
|