1

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

enter image description here

5
  • Nothing shell-specific (so it should work in not just BASH) is to use sed, the stream editor, to extract parts from a stream using regular expressions. Commented Jan 13, 2018 at 13:54
  • Where is the problem in your bash code? What is the text which you want to filter the digits out? What is the actual output and what is the desired output? Commented Jan 13, 2018 at 13:57
  • 1
    echo $string | tr -cd '[[:digit:]]' Commented Jan 13, 2018 at 14:02
  • In your case: temp="$(vcgencmd measure_temp | tr -cd '[[:digit:]]')" Commented Jan 13, 2018 at 14:03
  • @WilliamPursell Would you like to change your comment into an answer and add some explanation? That would get this out of the list of unanswered questions. Commented Jan 13, 2018 at 14:05

2 Answers 2

4

The typical tools for removing unwanted characters from text are tr and sed (and manipulating variables directly in the shell, with constructs like ${var##text}, but unless you want to use shell specific extensions those provide limited capability). For your use case, it seems easiest to do:

temp="$(vcgencmd measure_temp | tr -cd '[[:digit:]]')"

This simply deletes (-d) all characters that are not (-c) a member of the character class digit.

Sign up to request clarification or add additional context in comments.

2 Comments

Wow that works... However, In place of 65.9, I'm getting 659! That shows the red color!! That's 10 times higher! What should I do?
If you want to keep decimal points, include them in the list of characters that you don't want deleted. tr -cd '[[:digit:]].'
3

You could use a builtin string operation to print just the digits and periods in a variable. The following is a global substring replacement of any character that is not a digit or period with an empty string:

echo "${temp//[^0-9.]/}"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.