bar-cpu.sh 300 B

12345678910
  1. #!/usr/bin/env bash
  2. set -euo pipefail # bash strict mode
  3. read -r cpu a b c previdle rest < /proc/stat
  4. prevtotal=$((a+b+c+previdle))
  5. sleep 0.5
  6. read -r cpu a b c idle rest < /proc/stat
  7. total=$((a+b+c+idle))
  8. cpu=$((100*( (total-prevtotal) - (idle-previdle) ) / (total-prevtotal) ))
  9. echo -e CPU: "$cpu%"