You are not logged in.

#1 2016-07-11 02:15:11

pm6387
Member
Registered: 2012-11-19
Posts: 3

[SOLVED] Using Units for Conky CPU Temp

Hi folks,

My first time posting here - a bash newbie who would appreciate some help:

Having installed lm_sensors and units, I am trying to write a bash script to convert °C to °F using units - I want to display both °C and °F on conky.

The commands for obtaining the temperatures from sensors, and using Units to convert to °F work great on the command line, but have errors when run from within a shell script.

#!/bin/bash
# Must have 'units' utility installed.

# Build string array from output of "sensors" command
var=($(sensors))

# Get the 6th array element (CPU temperature in °C) with an optional minus (but not plus) sign 
c=${var[5]} | grep -Eo '[-]?[0-9]+([.][0-9]+)?'

#Convert to °F with units command
f="$(units "tempC($c)" "tempF")"

str=$c"°C/"$f"°F (High:70°C/158°F Critical:90°C/194°F)"

echo $str

Any help would be much appreciated!

Last edited by pm6387 (2016-07-17 13:07:25)

Offline

#2 2016-07-11 02:23:03

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,523
Website

Re: [SOLVED] Using Units for Conky CPU Temp

pm6387 wrote:

but have errors when run from within a shell script.

Are you going to tell us what the errors are?

You do know that `sensors` has the -f flag for farenheit?

I have no idea what you are trying to do with the line starting with 'c=' but there is clearly something wrong there.  Perhaps you are missing a $() wrapping the whole command and an echo command that you want to set c to.

Also, why do you think the temperature in C would be the 5th element of the array?  It most certainly isn't.  The 7th and 9th are temperatures.  That is unless you set the IFS to be just newlines.  Then the 5th element will *include* a temperature.

EDIT:

You might consider something more like the following:

awk 'NR==1 {printf "%s°C/", $4;} NR==3 {printf "%s°F (High:70°C/158°F Critical:90°C/194°F)\n", $4;}' <(acpi -t) <(acpi -tf)

This approach would also make it much easier to extend to use actual values for the high and critical temps rather than hardcoding them into the string:

#!/bin/bash

awk '
	NR==1 { c=$4; } NR==2 { cc=$12; } NR==3 { ch=$12; }
	NR==6 { f=$4; } NR==7 { fc=$12; } NR==8 { fh=$12; }
	END { printf "%s°C/%s°F (High:%s°C/%s°F Critical:%s°C/%s°F)\n", c, f, ch, fh, cc, fc; }
' <(acpi -ti) <(acpi -tif)

Last edited by Trilby (2016-07-11 02:53:11)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2016-07-14 23:46:50

pm6387
Member
Registered: 2012-11-19
Posts: 3

Re: [SOLVED] Using Units for Conky CPU Temp

Trilby,

Thanks much for the prompt response. And apologies for my delay in getting back - my computer was offline on unscheduled maintenance, and I wanted to look at the error again before getting back to you.

Trilby wrote:

Are you going to tell us what the errors are?

On running the script, the following error is seen at the terminal:

Error in 'tempC()': Parse error
°C/°F (High:70°C/158°F Critical:90°C/194°F)

It lloks like a bash error.

Trilby wrote:

You do know that `sensors` has the -f flag for farenheit?

I want to see both °C and °F on conky.

Trilby wrote:

why do you think the temperature in C would be the 5th element of the array?

My sensors command gives the following output, on which the 6th array element does indicate the CPU temperature:

kapalik@kalibari ~ $ sensors

k10temp-pci-00c3
Adapter: PCI adapter
temp1:        +35.8°C  (high = +70.0°C)
                       (crit = +80.0°C, hyst = +77.0°C)

fam15h_power-pci-00c4
Adapter: PCI adapter
power1:       27.14 W  (crit = 125.02 W)

radeon-pci-0100
Adapter: PCI adapter
temp1:        +34.0°C  (crit = +120.0°C, hyst = +90.0°C)

f71889ed-isa-0480
Adapter: ISA adapter
+3.3V:        +3.33 V  
in1:          +0.94 V  (max =  +2.04 V)
in2:          +1.09 V  
in3:          +0.88 V  
in4:          +0.62 V  
in5:          +1.27 V  
in6:          +1.58 V  
3VSB:         +3.31 V  
Vbat:         +3.25 V  
fan1:        1435 RPM
fan2:        1064 RPM
fan3:           0 RPM  ALARM
temp1:        +30.0°C  (high = +85.0°C, hyst = +81.0°C)
                       (crit = +80.0°C, hyst = +76.0°C)  sensor = transistor
temp2:        +44.0°C  (high = +85.0°C, hyst = +77.0°C)
                       (crit = +100.0°C, hyst = +92.0°C)  sensor = thermistor
temp3:        +33.0°C  (high = +70.0°C, hyst = +68.0°C)
                       (crit = +85.0°C, hyst = +83.0°C)  sensor = transistor
Trilby wrote:

awk 'NR==1 {printf "%s°C/", $4;} NR==3 {printf "%s°F (High:70°C/158°F Critical:90°C/194°F)\n", $4;}' <(acpi -t) <(acpi -tf)

On running this, I get the following:

bash: acpi: command not found
bash: acpi: command not found

...probably because I need to install some software. In any case, I am keen on using the units command because it is a linux utility which I would like to learn to use for different units. Also, I would like to use a bash script for getting the CPU temperature, since it could be extended in the future to get additional sensors data, without having to struggle with Conky syntax each time.

Thanks again for helping smile

Regards.

Offline

#4 2016-07-17 12:57:05

pm6387
Member
Registered: 2012-11-19
Posts: 3

Re: [SOLVED] Using Units for Conky CPU Temp

Hello All,

I was able to resolve the bash error; in case someone finds it useful, here is the correctly functioning bash script:

#!/bin/bash
# Must have 'units' utility installed.
# In Conky, set "override_utf8_locale yes" to make unicode ° symbol visible without error (a weird A preceding the symbol)
#************************************************************************


# Build string array from output of "sensors" command
var=($(sensors))

# Get the 6th array element (signed decimal value)
#echo ${var[5]} | grep -Eo '[+-]?[0-9]+([.][0-9]+)?'

# Get the 6th array element (CPU temperature in °C) with an optional minus (but not plus) sign 
#c=${var[5]} | grep -Eo '[-]?[0-9]+([.][0-9]+)?'
c="$(grep -Eo '[-]?[0-9]+([.][0-9]+)?' <<<"${var[5]}")"

#echo "\$c is $c"

#Convert to °F with units command
f="$(units "tempC($c)" "tempF")"

#echo $f

str=$c
deg=$'\xc2\xb0'  #UTF-8 (hex) sequence for degree symbol (http://www.fileformat.info/info/unicode/char/b0/index.htm)
str+=$deg
str+="C/"
str+=$f
#str+="°F (High:70°C/158°F Critical:80°C/176°F)"
str+=$deg
str+="F (High:70"
str+=$deg
str+="C/158"
str+=$deg
str+="F Critical:80"
str+=$deg
str+="C/176"
str+=$deg
str+="F)"

echo $str

Regards.

Offline

Board footer

Powered by FluxBB