Updated CPU frequency scaling script

This is an update to my earlier command line frequency scaling script. This version uses the dialog utility to provide a rudimentary GUI.

  1. #!/bin/bash
  2. # Wrapper to easily set the CPU Frequency Governor
  3. # dialog is a utility installed by default on all major Linux distributions.
  4. # But it is good to check availability of dialog utility on your Linux box.
  5.  
  6. which dialog &> /dev/null
  7.  
  8. [ $? -ne 0 ]  && echo "Dialog utility is not available, please install it" && exit 1
  9.  
  10. # Check that a valid governor is set
  11.  
  12. # Creating an array with each of the CPUs numeric ID
  13. declare -a  CPUs=( `cat /proc/cpuinfo |grep processor |cut -f 2 -d \:` )
  14.  
  15. # Setting up the dialogue interface:
  16.  dialog --clear --backtitle "Console Based CPU Governor Selection" --title "MAIN MENU" \
  17.     --menu "Use [UP/DOWN] key to move" 18 100 6 \
  18.     "performance" "Set for maximum performance, watch the temp!" \
  19.     "ondemand"  "Usually the default, scale speed up as required." \
  20.     "conservative"    "Conservatively scale up as required." \
  21.     "powersave"     "Maximum power savings or lowest temp." \
  22.     "userspace"      "Send control to a userspace application." \
  23.     "EXIT"      "TO EXIT" 2> menuchoices.$$
  24.  
  25. # This computes the number of elements in the array, we use this to control our loop in the SetSpeed function
  26. elements="${#CPUs[*]}"
  27.  
  28. SetSpeed ()
  29. {  
  30.   for (( i = 0  ; i < $elements ; i++ ))
  31.   do sudo cpufreq-set -c ${CPUs[$i]} -g $GOVERNOR
  32. done
  33. }
  34.  
  35. retopt=$?
  36.     choice=`cat menuchoices.$$`
  37.  
  38.     case $retopt in
  39.  
  40.            0) case $choice in
  41.  
  42.                   performance)  GOVERNOR=performance ; SetSpeed ;;
  43.                   ondemand)   GOVERNOR=ondemand ; SetSpeed  ;;
  44.                   conservative)     GOVERNOR=conservative ; SetSpeed  ;;
  45.                   powersave)      GOVERNOR=powersave ; SetSpeed  ;;
  46.                   userspace)       GOVERNOR=userspace ; SetSpeed ;;
  47.                   EXIT)       clear; exit 0;;
  48.  
  49.               esac ;;
  50.  
  51.           *)clear ; exit ;;
  52.     esac