I want to measure temperature of Raspberry pi 3, and change the background color of the text accordingly. As the code goes, we can print the temperature on the display. Now, I want to filter the digits only out of the text. And use that in the if condition. The source code goes like this:
#!/bin/bash
measurement()
{
i=1
echo "Reading Temperature"
echo "Updating time is set to $1"
while [ $i -eq 1 ]; do
temp="$(vcgencmd measure_temp | cut -d= -f 2 | cut -d\' -f 1)"
tput setaf 7
if [[ $temp -ge 70 ]]; then
tput setab 1
echo -ne "Temperature = $temp\r"
elif [[ $temp -ge 65 && $temp -le 69 ]]; then
put setab 3
echo -ne "Temperature = $temp\r"
elif [[ $temp -ge 60 && $temp -le 64 ]]; then
tput setab 2
echo -ne "Temperature = $temp\r"
elif [[ $temp -ge 55 && $temp -le 59 ]]; then
tput setab 6
echo -ne "Temperature = $temp\r"
else
tput setab 4
echo -ne "Temperature = $temp\r"
fi
sleep $1
done
}
if [ -n "$1" ]; then
sleep_time=$1
measurement $sleep_time
else
read -p "Enter the Update Time: " sleep_time
measurement $sleep_time
fi
sed, the stream editor, to extract parts from a stream using regular expressions.echo $string | tr -cd '[[:digit:]]'temp="$(vcgencmd measure_temp | tr -cd '[[:digit:]]')"