Many unix tools that report sizes in bytes can convert them to either IEC or SI readable formats.
For github.com/jpluimers/btrfs-du/blob/master/btrfs-du I wrote about last week, I also wanted that kind of behaviour. So I did some research and came up with the code and test cases below.
Note that depending on the bitness of your system, bash integer numeric values are limited in size; see [WayBack] What is the maximum value of a numeric bash shell variable? – Super User.
So I wrote a small bash
script for that too, which needed also gave me the opportunity to show how a perpetual while loop as explained by [WayBack] bash – “while :” vs. “while true” – Stack Overflow.
Two things that always bite me with these short scripts are expressions (done through [WayBack]Arithmetic Expansion) and comparisons (through[WayBack] Other Comparison Operators).
The IEC suffixes contain one extra i
to indicate binary and – next to the ISO notation that were already ISO defined – made it into the ISO 80000 standard since 2008. Here is a comparison list from [WayBack] Binary prefix – Wikipedia:
Prefixes for multiples of
bits (bit) or bytes (B) |
|
|
Most tools nowadays default to binary IEC suffixes for byte sizes, though disk manufacturers still use SI suffixes because, well then they appear bigger but aren’t. Just for comparison, look at the numbers from [WayBack] File size – Wikipedia and [WayBack] IEC and SI Size Notations – AN!Wiki where I got the test cases from:
Read the rest of this entry »
Like this:
Like Loading...