LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-14-2016, 08:32 PM   #1
pm6387
LQ Newbie
 
Registered: Jan 2011
Distribution: Arch Linux LTS
Posts: 7

Rep: Reputation: 9
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.8C  (high = +70.0C)
                       (crit = +80.0C, hyst = +77.0C)

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

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

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.0C  (high = +85.0C, hyst = +81.0C)
                       (crit = +80.0C, hyst = +76.0C)  sensor = transistor
temp2:        +44.0C  (high = +85.0C, hyst = +77.0C)
                       (crit = +100.0C, hyst = +92.0C)  sensor = thermistor
temp3:        +33.0C  (high = +70.0C, hyst = +68.0C)
                       (crit = +85.0C, hyst = +83.0C)  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:70C/158F Critical:80C/176F)"

echo $str
On running this in a terminal, I get:
Code:
Error in 'tempC()': Parse error
C/Error in 'tempC()': Parse errorF (High:70C/158F Critical:80C/176F)
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.
 
Old 07-15-2016, 01:25 PM   #2
hydrurga
Senior Member
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 18.2 MATE
Posts: 3,490
Blog Entries: 2

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Out of interest, what does the echo $f produce?
 
Old 07-15-2016, 01:28 PM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 5,940

Rep: Reputation: 1409Reputation: 1409Reputation: 1409Reputation: 1409Reputation: 1409Reputation: 1409Reputation: 1409Reputation: 1409Reputation: 1409Reputation: 1409
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:70C/158F Critical:80C/176F)"

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
 
1 members found this post helpful.
Old 07-15-2016, 07:16 PM   #4
pm6387
LQ Newbie
 
Registered: Jan 2011
Distribution: Arch Linux LTS
Posts: 7

Original Poster
Rep: Reputation: 9
Smile 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.
 
Old 07-15-2016, 07:20 PM   #5
pm6387
LQ Newbie
 
Registered: Jan 2011
Distribution: Arch Linux LTS
Posts: 7

Original Poster
Rep: Reputation: 9
Smile Re: what does the echo $f produce?

hydrurga,

$f uses the units (linux) utility to convert C to F.
 
Old 07-16-2016, 01:36 AM   #6
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 5,940

Rep: Reputation: 1409Reputation: 1409Reputation: 1409Reputation: 1409Reputation: 1409Reputation: 1409Reputation: 1409Reputation: 1409Reputation: 1409Reputation: 1409
Quote:
Originally Posted by pm6387 View Post
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).
 
Old 07-17-2016, 08:16 AM   #7
pm6387
LQ Newbie
 
Registered: Jan 2011
Distribution: Arch Linux LTS
Posts: 7

Original Poster
Rep: Reputation: 9
Smile

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:70C/158F Critical:80C/176F)"
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Error in Bash: line 77: syntax error: unexpected end of file bribon Programming 8 07-13-2011 12:43 PM
bash: syntax error denneyd Linux - Software 3 04-08-2010 08:41 PM
syntax error in bash tugce_zehra Linux - Newbie 2 12-05-2008 09:59 AM
bash, syntax error santana Linux - Software 2 01-23-2008 01:39 PM
BASH : syntax error semaja2 Programming 6 01-26-2006 12:33 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration