The little things give you away... A collection of various small helper stuff
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

38 lines
760 B

  1. #!/bin/bash
  2. # 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
  3. {
  4. if [ ! -t 0 ]; then cat; fi
  5. echo "$@"
  6. } |
  7. tr '\t ' '\n' |
  8. awk '
  9. BEGIN {
  10. units[0] = "B";
  11. units[1] = "KiB";
  12. units[2] = "MiB";
  13. units[3] = "GiB";
  14. units[4] = "TiB";
  15. units[5] = "PiB";
  16. }
  17. {
  18. size += $1;
  19. }
  20. END {
  21. if (size > 0) {
  22. magnitude = int(log(size) / log(1024));
  23. if (magnitude > 5) {
  24. magnitude = 5;
  25. }
  26. } else {
  27. magnitude = 0;
  28. }
  29. if (magnitude > 0) {
  30. sizeformat = "%.2f";
  31. } else {
  32. sizeformat = "%d";
  33. }
  34. printf sizeformat " %s\n", size / (1024 ^ magnitude), units[magnitude];
  35. }'