Search code examples
bashcpu

Bash Script to Monitor CPU clock speed


I made a simple program but it prints all CPU speeds on ONE line... Any suggestions how to make each CPU print on its own line?

#!/bin/bash

while [ 1 ]
    do 
        my_array=( $(cat /proc/cpuinfo | grep MHz) )
        printf '%s\033[A\n%s\033[A\n%s\033[A\n%s\033[A\n' "${my_array[*]}"
        sleep 1
    done



Solution

  • You could use

    #!/bin/bash
    
    while [ 1 ]
        do.
            IFS="\n"
            my_array=( $(cat /proc/cpuinfo | grep MHz) )
            for item in ${my_array[*]}
             do
              echo ${item}
              sleep 1
              clear
            done
        done
    

    Pay attention to the IFS variable. IFS=$'\n' (newline) is required to interpret each line from the multiline grep output as a new array item.

    By default, IFS is [::space::] or not defined.