Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Created By Marc Gilligan
- # https://github.com/ulfnic
- # BSD-0 or CC-0
- print_size() {
- local -a types=(B KiB MiB GiB TiB PiB EiB ZiB YiB)
- local num=$1
- local \
- num_len=${#num} \
- thousands digits
- thousands=$(((num_len - 1) / 3))
- ((thousands >= ${#types[@]})) && return 1
- digits=$((num_len - (thousands * 3) + 1))
- if ((thousands > 0)); then
- printf ' %*s%s %s\n' 4 "${num:0:digits}" ".${num:digits:1}" "${types[thousands]}"
- else
- printf ' %*s %s\n' 6 "${num:0:digits}" "${types[thousands]} "
- fi
- }
- print_size 1
- print_size 12
- print_size 123
- print_size 1234
- print_size 12345
- print_size 123456
- print_size 1234567
- print_size 12345678
- print_size 123456789
- print_size 1234567890
- print_size 12345678901
- print_size 123456789012
- print_size 1234567890123
- print_size 12345678901234
- print_size 123456789012345
- print_size 1234567890123456
- print_size 12345678901234567
- print_size 123456789012345678
- print_size 1234567890123456789
- print_size 12345678901234567890
- print_size 123456789012345678901
- print_size 1234567890123456789012
- print_size 12345678901234567890123
- print_size 123456789012345678901234
- print_size 1234567890123456789012345
- print_size 12345678901234567890123456
- print_size 123456789012345678901234567
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement