Updated Bash CPU Meter

In August, I did a post on a pretty simple bash cpu meter. This one is still Intel-only, but is records the range of frequencies used during a report.

#!/bin/bash
COLWID=17
MAXTURBO=4200
function find_lines {
   local i=0
   local j=0
   while read line; do
      if [[ $line =~ cpu\ MHz ]]; then
         cpu[i]=$j
         ((i++))
      fi
      ((j++))
   done < /proc/cpuinfo
}
function get_mhz() {
   mhz=()
   local cpulines=()
   local line hunks m L i c
   for i in `seq 1 15`; do
      c=0;
      readarray cpulines < /proc/cpuinfo
      for L in "${cpu[@]}"; do
          line="${cpulines[$L]}"
          hunks=($line)
          m=${hunks[3]}
          mhz[c]+="${m%.*} "
          ((c++))
       done
       sleep 0.1s
    done
}
# main
find_lines
while [[ 1 = 1 ]]; do
    COLS=`tput cols`
    mhz=()
    get_mhz
    cpunum=0
    for A in ${cpu[@]}; do
       lowest=0
       highest=0
       for H in ${mhz[$cpunum]}; do
         (( $H > $highest)) && highest=$H
         (( $lowest == 0    )) && lowest=$H
         (( $H < $lowest )) && lowest=$H
      done      
      outline=""      
      bars=$(( ($lowest * ($COLS-$COLWID) )/$MAXTURBO))
      for (( L=1; L<=$bars; L++ )); do
         outline=$outline"-"
      done
      bars=$(( (((1+$highest)-$lowest) * ($COLS-$COLWID))/$MAXTURBO))
      for (( L=1; L<=$bars; L++ )); do
         outline=$outline"="
      done
      d=$(($cpunum+9900))
      echo "${d##99} $lowest-$highest $outline"
      ((cpunum++))
   done
   echo ""
   sleep 0.1
done
#

4 responses to “Updated Bash CPU Meter”

  1. how do you run this script?

    I tried it and getting this response in cmd line:

    # ./cpuMeter.bash
    00 2394-2394 ——————————————————————————-
    01 2394-2394 ——————————————————————————-
    02 2394-2394 ——————————————————————————-
    03 2394-2394 ——————————————————————————-

    00 2394-2394 ——————————————————————————-
    01 2394-2394 ——————————————————————————-
    02 2394-2394 ——————————————————————————-
    03 2394-2394 ——————————————————————————-

    00 2394-2394 ——————————————————————————-
    01 2394-2394 ——————————————————————————-
    02 2394-2394 ——————————————————————————-
    03 2394-2394 ——————————————————————————-

  2. sh cpu_meter.sh
    cpu_meter.sh: 4: cpu_meter.sh: function: not found
    cpu_meter.sh: 5: local: not in a function

%d bloggers like this: