Jeffrey.j wrote: ↑Sun May 27, 2018 12:05 am
PhilE wrote: ↑Tue Apr 18, 2017 2:00 pm
Code: Select all
#!/bin/bash
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)
echo "$(date) @ $(hostname)"
echo "-------------------------------------------"
echo "GPU => $gpu'C ($gpuf'F)"
echo "CPU => $cpu'C ($cpuf'F)"
Hi. I know this is an older post but I was hoping that you could briefly explain these parts:
That is a very simple awk program that splits each line from the input file into fields where fields are delimited by either an equals sign or a single quote and outputs the second field.
-F "[=\']" sets the field separator, if you give a string of more than one character then the string is a regular expression.
'{print $2}' is the awk program to run. No pattern is given so it will be applied to every line, $
n means the item that is the
nth field.
Jeffrey.j wrote: ↑Sun May 27, 2018 12:05 am
Code: Select all
cpu=$(echo "$cpu / 100 * 0.1" | bc)
(Just why this one is nessessary), and why is there a “<“ here:
Code: Select all
cpu=$(</sys/class/thermal/thermal_zone0/temp)
. Also, if I were to make this an alias, how would I do that? Thank you.
$(<filename) says return the contents of the file
filename, here the content of that file is a string (e.g. 49388) so the variable
cpu will have that string. Think of it as
$(cat filename), just that bash will do it itself rather than calling cat to do it.
The temperature from that file is the temperature*1000.
$(echo "$cpu / 100 * 0.1" | bc) passes the equation (using the value above) "49388 / 100 * 0.1" to the calculator program
bc. It isn't just divided by 1000 because in
bc the divide operator returns the quotient so 49388 / 100 will give the answer 493 (the 0.88 is discarded), this is then multiplied by 0.1 to effectively divide by a further 10 to get the answer but this time keeping the fractional part to give 49.3