utils.sh 503 B

12345678910111213141516171819202122232425262728293031
  1. gibibytes_to_bytes()
  2. {
  3. echo $(( 1024 * 1024 * 1024 * "${1}" ))
  4. }
  5. mebibytes_to_bytes()
  6. {
  7. echo $(( 1024 * 1024 * "${1}" ))
  8. }
  9. kibibytes_to_bytes()
  10. {
  11. echo $(( 1024 * "${1}" ))
  12. }
  13. bytes_to_gibibytes()
  14. {
  15. echo $(( "${1}" / 1024 / 1024 / 1024 ))
  16. }
  17. to_bytes()
  18. {
  19. case "${1: -1}" in
  20. b) echo "${1:0: -1}" ;;
  21. [0-9]) echo "${1}" ;;
  22. K) kibibytes_to_bytes "${1:0: -1}" ;;
  23. M) mebibytes_to_bytes "${1:0: -1}" ;;
  24. G) gibibytes_to_bytes "${1:0: -1}" ;;
  25. *) fatal UNIT_E "${1:-1} is not a valid unit" ;;
  26. esac
  27. }