LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Please help with Bash syntax error (https://www.linuxquestions.org/questions/linux-newbie-8/please-help-with-bash-syntax-error-4175584680/)

pm6387 07-14-2016 08:32 PM

Please help with Bash syntax error
 
Hello all,

I have Arch Linux, and am trying to write a bash script to display the CPU Temperature on Conky in °C as well as °F

My sensors command output is:

Code:

user@host ~ $ 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

The scipt I am trying to create:
Code:

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

:<<'COMMENTBLOCK'
***********************************************************
http://linux.die.net/man/1/units
http://www.tldp.org/LDP/abs/html/abs-guide.html#UNITSREF

convert_units ()  # Takes as arguments the units to convert.
{
  cf=$(units "$1" "$2" | sed --silent -e '1p' | awk '{print $2}')
  # Strip off everything except the actual conversion factor.
  echo "$cf"


Unit1=miles
Unit2=meters
cfactor=`convert_units $Unit1 $Unit2`
quantity=3.73

result=$(echo $quantity*$cfactor | bc)

echo "There are $result $Unit2 in $quantity $Unit1."

***********************************************************
COMMENTBLOCK


# 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]+)?'

echo $c

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

echo $f

str=$c
str+="°C/"
str+=$f
str+="°F (High:70°C/158°F Critical:80°C/176°F)"

echo $str

On running this in a terminal, I get: :scratch:
Code:

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

I am keen on using the units command because it is a linux utility that 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 in advance.

Regards.

hydrurga 07-15-2016 01:25 PM

Out of interest, what does the echo $f produce?

ondoho 07-15-2016 01:28 PM

fixed it, see if you can spot your mistake?
Code:

#!/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="$(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
str+="°C/"
str+=$f
str+="°F (High:70°C/158°F Critical:80°C/176°F)"

echo $str

btw, i read temperatures directly from /sys/devices/platform/coretemp.0/hwmon/hwmon1/, no need to use "sensors" for that.
here's my shell script:
Code:

#!/bin/dash

warn=60
max=80
crit=100

read temp </sys/devices/platform/coretemp.0/hwmon/hwmon1/$1_input
read templabel </sys/devices/platform/coretemp.0/hwmon/hwmon1/$1_label

temp=${temp%000}

if [ "$temp" -ge "$crit" ] ; then
                echo -n "\$color9"
        elif  [ "$temp" -ge "$max" ] ; then
        echo -n "\${color8}"
        elif [ "$temp" -ge "$warn" ] ; then
                echo -n "\${color7}"
        else echo -n "\${color}"
fi
echo -n "$templabel :\${alignr}${temp}°C"

and here's a snippet from my conky:
Code:

${execp ~/.config/conky/sys/cpu-conky-temp.sh temp1}
$color${execp ~/.config/conky/sys/cpu-conky-temp.sh temp2}
$color${execp ~/.config/conky/sys/cpu-conky-temp.sh temp3}$color


pm6387 07-15-2016 07:16 PM

Thanks to ondoho!!!
 
ondoho,

Thanks so much for correcting the error. I did see the line that you changed, but being a bash newbie, couldn't figure out the exact nature of the error. Guess I will figure it out eventually.

Thanks also for the hwmon1 script and the corresponding conky snippet; I will certainly check it out.

Regards.

pm6387 07-15-2016 07:20 PM

Re: what does the echo $f produce?
 
hydrurga,

$f uses the units (linux) utility to convert °C to °F.

ondoho 07-16-2016 01:36 AM

Quote:

Originally Posted by pm6387 (Post 5576688)
I did see the line that you changed, but being a bash newbie, couldn't figure out the exact nature of the error. Guess I will figure it out eventually.

originally, it was this:
Code:

c=${var[5]} | grep -Eo '[-]?[0-9]+([.][0-9]+)?'
echoing c after that returns - nothing.
the quickest and most obvious way to fix it would be like this:
Code:

c=`echo ${var[5]} | grep -Eo '[-]?[0-9]+([.][0-9]+)?'`
but i don't like backticks, so i used $(...) instead, and i don't like pipes, so i used <<<"$..." instead (careful, that's a bashism).

pm6387 07-17-2016 08:16 AM

Hello All,

Thanks again to ondoho for the help in resolving this issue.

In case someone finds it useful, here is the properly functioning bash script:

Code:

#!/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/unic...r/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

:<<'COMMENTBLOCK'
***********************************************************
Alternate way of using the units utility
***********************************************************
http://linux.die.net/man/1/units
http://www.tldp.org/LDP/abs/html/abs....html#UNITSREF

convert_units ()  # Takes as arguments the units to convert.
{
  cf=$(units "$1" "$2" | sed --silent -e '1p' | awk '{print $2}')
  # Strip off everything except the actual conversion factor.
  echo "$cf"


Unit1=miles
Unit2=meters
cfactor=`convert_units $Unit1 $Unit2`
quantity=3.73

result=$(echo $quantity*$cfactor | bc)

echo "There are $result $Unit2 in $quantity $Unit1."

***********************************************************
Find CPU Temp with hwmon1 & corresponding conky snippet
***********************************************************
#!/bin/dash

warn=60
max=80
crit=100

read temp </sys/devices/platform/coretemp.0/hwmon/hwmon1/$1_input
read templabel </sys/devices/platform/coretemp.0/hwmon/hwmon1/$1_label

temp=${temp%000}

if [ "$temp" -ge "$crit" ] ; then
                echo -n "\$color9"
        elif  [ "$temp" -ge "$max" ] ; then
        echo -n "\${color8}"
        elif [ "$temp" -ge "$warn" ] ; then
                echo -n "\${color7}"
        else echo -n "\${color}"
fi
echo -n "$templabel :\${alignr}${temp}°C"


${execp ~/.config/conky/sys/cpu-conky-temp.sh temp1}
$color${execp ~/.config/conky/sys/cpu-conky-temp.sh temp2}
$color${execp ~/.config/conky/sys/cpu-conky-temp.sh temp3}$color

***********************************************************
#http://conky.sourceforge.net/faq.html
#Q: How can I make Conky print the degree symbol (°) correctly?
#A: This is a Problem of character encoding and possibly a bug. Until it has been fixed, there exists a workaround when Conky was built with iconv support. Having a system defaulting to UTF-8, the following could do the trick:
#${iconv_start UTF-8 ISO_8859-1}° ${iconv_stop}

***********************************************************
COMMENTBLOCK



All times are GMT -5. The time now is 04:36 AM.