35-diskspace 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/bin/bash
  2. # config
  3. max_usage=90
  4. bar_width=50
  5. # colors
  6. white="\e[39m"
  7. green="\e[1;32m"
  8. red="\e[1;31m"
  9. dim="\e[2m"
  10. undim="\e[0m"
  11. # disk usage: ignore zfs, squashfs & tmpfs
  12. mapfile -t dfs < <(df -H -x zfs -x squashfs -x tmpfs -x devtmpfs --output=target,pcent,size | tail -n+2)
  13. printf "\ndisk usage:\n"
  14. for line in "${dfs[@]}"; do
  15. # get disk usage
  16. usage=$(echo "$line" | awk '{print $2}' | sed 's/%//')
  17. used_width=$((($usage*$bar_width)/100))
  18. # color is green if usage < max_usage, else red
  19. if [ "${usage}" -ge "${max_usage}" ]; then
  20. color=$red
  21. else
  22. color=$green
  23. fi
  24. # print green/red bar until used_width
  25. bar="[${color}"
  26. for ((i=0; i<$used_width; i++)); do
  27. bar+="="
  28. done
  29. # print dimmmed bar until end
  30. bar+="${white}${dim}"
  31. for ((i=$used_width; i<$bar_width; i++)); do
  32. bar+="="
  33. done
  34. bar+="${undim}]"
  35. # print usage line & bar
  36. echo "${line}" | awk '{ printf("%-31s%+3s used out of %+4s\n", $1, $2, $3); }' | sed -e 's/^/ /'
  37. echo -e "${bar}" | sed -e 's/^/ /'
  38. done