1

I have this alias to check the temperature of my Raspberry Pi:

gpu=$(/opt/vc/bin/vcgencmd measure_temp | awk -F "[= \']" '{print $2}')

cpu=$(</sys/class/thermal/thermal_zone0/temp)

cpu=$(echo "$cpu / 100 * 0.1" | bc)

cpuf=$(echo "(1.8 * $cpu) + 32" | bc)

gpuf=$(echo "(1.8 * $gpu) + 32" | bc)

alias temp="echo \"CPU => $cpuf 'F\" && echo \"GPU => $gpuf 'F\""

It is running Ubuntu Mate 16.04. It works well, but when I want to check the temp, I have to close the terminal I have open and open a new one. E.g. After I execute temp once in a terminal, it stores that data until I close and reopen the terminal. I was hoping there was a way to have the alias refresh each time I execute it. This alias is in /etc/bash.bashrc Thank you!

  • Does it have to be an alias? – Jesse_b May 30 '18 at 18:37
  • 2
    Why not just use a function? – Ignacio Vazquez-Abrams May 30 '18 at 18:37
  • 1
    What you have there, are five assignments and one alias definition. The two first assignments seem to run commands that actually fetch the temperatures, the alias just prints the stored values. So, assuming all that is in your shell's startup file, the temperature values get stored when the shell starts, not when you run the alias. – ilkkachu May 30 '18 at 18:44
4

This sounds like it would be better suited as a function. (Which can/should also be added to your bashrc and simulates a simple command much like an alias):

temp () {
    gpu=$(/opt/vc/bin/vcgencmd measure_temp | awk -F "[= \']" '{print $2}')
    cpu=$(</sys/class/thermal/thermal_zone0/temp)
    cpu=$(bc <<<"$cpu / 100 * 0.1")
    cpuf=$(bc <<<"(1.8 * $cpu) + 32")
    gpuf=$(bc <<<"(1.8 * $gpu) + 32")

    echo -e "CPU => $cpuf °F\nGPU => $gpuf °F"
}
  • 1
    Trying to emulate "℉". – Ignacio Vazquez-Abrams May 30 '18 at 18:43
  • printf 'CPU => %s F\nGPU => %s F\n' "$cpuf" "$gpuf" – Kusalananda May 30 '18 at 18:50
  • You could even turn the C->F conversion into its own function and call that in temp, but that may make it too "big" for such a simple task. – Kusalananda May 30 '18 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.