1

I have a raspberry pi 4 with Ubuntu 20.04 installed. I'm new to linux and trying to log the temperature of the raspberry pi every few minutes.

I wrote a .sh file that I am trying to run but is not working right and I am not sure why.

#!/bin/bash
temp=$(/sys/class/thermal/thermal_zone0/temp)
temp_f=echo "$temp/1000" | bc -l
echo "Pi Temp: %.3fC\n" $temp_f >> /home/projects/temperature_logger/temp.log

When I try running this I get

./logscript.sh: line 2: /sys/thermal/thermal_zone0/temp: Permission Denied
./logscript.sh: line 3: /1000: No such file or directory
./logscript.sh: line 4: /home/projects/temperature_logger/temp.log: Permission Denied

How can I resolve these permission issues? I am logged in as ubuntu who is the only user on this pi.

bjk116
  • 121

1 Answers1

3
  • 1st line tries to execute /sys/.../temp. That is not what you want. Use cat or head -n 1 to retrieve the value in that file.

  • 2nd line lacks the $() construct to evaluate the command and store its output.

  • 3rd line complains about permission. You need write permission to the temp.log file, which can be granted with chmod ugo+w temp.log.

I have also replaced echo by printf as the latter is more reliable and in the last line that is how you truncate the value to three decimal places.

temp=$(cat /sys/class/thermal/thermal_zone0/temp)
temp_f=$(printf '%s\n' "$temp/1000" | bc -l)
printf 'Pi Temp: %.3fC\n' "$temp_f" >> /home/projects/temperature_logger/temp.log
Quasímodo
  • 18,865
  • 4
  • 36
  • 73