0

I am trying to store CPU temperature in a variable. I am not sure how to use sed so I am mostly using grep. Here is what I have tried so far, but I am getting syntax error. I am also getting error during the comparison, but I thing it is because CPUTEMP is not resolving.

#!/bin/bash
#
CPUTEMP = $((/usr/bin/sensors k10temp-pci-00c3 | grep temp1 | awk '{print \$2}' | grep -o '[0-9,.]\+'))

if [ "$CPUTEMP" -le 40 ]; then
    echo "OK - CPU idle"
    exit 0
...
...
fi

What am I doing wrong here? Thank you!

Amar
  • 43
  • 5

1 Answers1

0

I read some more online and was able to solve my problem. There were four problems in my script.

  1. I used $(( ... )) instead of $( ... )
  2. There were spaces before and after =
  3. No need for \ before $2

(all of these problems above are in the same line where CPUTEMP is assigned a value)

  1. Bash can only compare integers, but my CPUTEMP variable contains floating point number. Need bc for the workaround.

Here is the working code:

#!/bin/bash
#
CPUTEMP=$(/usr/bin/sensors k10temp-pci-00c3 | grep temp1 | awk '{print $2}' | grep -o '[0-9,.]\+')

if (( $(echo "$CPUTEMP <= 40" | bc -l) ))
then
    echo "OK - CPU idle"
    exit 0
...
...
fi

Thank you for your help guys!

Amar
  • 43
  • 5