fredex
Posts: 20
Joined: Mon Apr 10, 2017 1:39 am

silly little script for RPi

Sun Apr 23, 2017 11:42 pm

This bash script grabs the CPU temp reading and reformats it into degrees and decimal fraction. I know it's dumb, but someone (besides me) might find it useful:

Code: Select all

cat /usr/local/bin/checktemp
#!/bin/sh

temp=`cat /sys/class/thermal/thermal_zone0/temp`
foo=$((${temp}/1000))
bar=$((${temp}%1000))
echo ${foo}.`printf %.3d ${bar}` C

wh7qq
Posts: 1448
Joined: Thu Oct 09, 2014 2:50 am

Re: silly little script for RPi

Mon Apr 24, 2017 9:47 pm

Good but

Code: Select all

vcgencmd measure_temp
returns the same value, nicely formatted.

fredex
Posts: 20
Joined: Mon Apr 10, 2017 1:39 am

Re: silly little script for RPi

Fri Apr 28, 2017 5:49 pm

thanks for the info. I would have never (or at least not for a long time) known that command even existed.

Sadly, I note that there is neither a man page for it, nor built-in help. googling for it turns up a lot of pages, though, which is helpful once I knew of its existence.

but for this specific use, I still like my shellscript: it requires less typing! :)

mikerr
Posts: 2825
Joined: Thu Jan 12, 2012 12:46 pm
Location: UK
Contact: Website

Re: silly little script for RPi

Fri Apr 28, 2017 6:01 pm

The shell script has the advantage of being the standard linux way, and will work on other devices e.g. PC's mostly unchanged.

Vcgencmd is raspberry pi specific/proprietary .
Android app - Raspi Card Imager - download and image SD cards - No PC required !

User avatar
rpdom
Posts: 17172
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: silly little script for RPi

Fri Apr 28, 2017 6:29 pm

The scripts works well, that is the important thing. The use of /bin/sh will make it compatible with other Unix-like operating systems as well, but they may not support the /sys/class/thermal system.

If I were writing it, I'd make it use Bash and do things slightly differently (I think everyone has their own ideas on how to do things :-) )

I'd use the ((...)) bashism for the maths, combine the echo and printf into one printf command and use the bash $(...) construct instead of the `...` backticks, as it is more flexible and can be easier to read (as well as supporting nested commands)

Code: Select all

#!/bin/bash
temp=$(cat /sys/class/thermal/thermal_zone0/temp)
((foo = temp / 1000))
((bar = temp % 1000))
printf "%d.%.3d\302\260C\n" $foo $bar
Disclaimer: The "\302\260" prints a degree sign (°) on my systems, it may not work on others.

MacsandaPi
Posts: 103
Joined: Tue Dec 30, 2014 7:44 pm

Re: silly little script for RPi

Fri Apr 28, 2017 9:29 pm

The following script combines input from both vcgencmd measure_temp and thermal_zone. Strangely, the two temperatures are often slightly different. Are there two temperature sensors on the chip?

Code: Select all

#!/bin/bash
cputemp=$(/opt/vc/bin/vcgencmd measure_temp)
echo CPU temp from vcgencmd: $cputemp
cputherm=$(cat /sys/class/thermal/thermal_zone0/temp)
ctherm=$(echo "scale=1;$cputherm/1000" | bc)
ftherm=$(echo "scale=1;(($cputherm/1000)*(9/5))+32" | bc)

MacsandaPi
Posts: 103
Joined: Tue Dec 30, 2014 7:44 pm

Re: silly little script for RPi

Fri Apr 28, 2017 9:40 pm

Oops...I omitted a final line in the previous post's script. Full script should be:

Code: Select all

#!/bin/bash
cputemp=$(/opt/vc/bin/vcgencmd measure_temp)
echo CPU temp from vcgencmd: $cputemp

cputherm=$(cat /sys/class/thermal/thermal_zone0/temp)
ctherm=$(echo "scale=1;$cputherm/1000" | bc)
ftherm=$(echo "scale=1;(($cputherm/1000)*(9/5))+32" | bc)

echo CPU temp from thermal_zone: $ctherm"ºC ($fthermºF)"

fredex
Posts: 20
Joined: Mon Apr 10, 2017 1:39 am

Re: silly little script for RPi

Mon Nov 13, 2017 2:10 am

kinda late, but for what its worth (not much...) here's my latest checktemp script, including suggestions made by other posters:

#!/bin/bash

temp=`cat /sys/class/thermal/thermal_zone0/temp`
((foo = temp / 1000))
((bar = temp % 1000))
printf "%s temp=%d.%.3d\302\260C\n" `date +%x-%T` $foo $bar

as mentioned in another posting above, the "\302\206" produces a degree symbol on a UTF-8 terminal.

MaxK1
Posts: 1043
Joined: Sun Aug 26, 2012 11:34 pm

Re: silly little script for RPi

Mon Nov 13, 2017 7:56 am

For extra credit, display the temp in Fahrenheit, Kelvin, etc.. as well
You are in a maze of twisty little passages, all alike.
When General Failure and Major Disaster get together, Private Parts usually suffers.

jahboater
Posts: 5759
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: silly little script for RPi

Mon Nov 13, 2017 8:27 am

How about you add this line to ~/.bashrc ?

alias checktemp='vcgencmd measure_temp'

Return to “Beginners”