LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-18-2006, 09:50 AM   #1
eldaria
LQ Newbie
 
Registered: Feb 2004
Posts: 22

Rep: Reputation: 15
Question Bash Script, calculate output.


Hey guys.

First of all, being a previous Microsoft Slave, being pretty good at VB, I got fed up and pretty much completely switched all my PC's to Kubuntu and my Server from Win2k to Ubuntu Server, I am slowly learning. but well luckily there is a great community to guide me. :-)

I'm running Ubuntu Server with lm-sensors installed.
For Cacti I would like to extract the raw temperature and Fan values without the fancy formatting from the sensors command.

So after a lot of searching and testing in /dev, /proc and /sys, I finally found that they are stored in /sys/bus/i2c/devices/9191-0290/

So to get my value I typed.
Code:
cat /sys/bus/i2c/devices/9191-0290/temp1_input
47500
Now as you can probably guess my motherboard is not running 47500 degrees celcius, :-)

So after some searching on Google I found a bash script that does calculations.
Code:
#!/bin/bash
#
a=5
b=10
c=`echo $a + $b | bc`
echo "$a + $b = $c"
But since it is not a fixed value I want to calculate I tried to modify the script.

Code:
#!/bin/bash
#
a=cat /sys/bus/i2c/devices/9191-0290/temp1_input
b=1000
c=`echo $a / $b | bc`
echo "$a / $b = $c"
But that gives me following error:

Code:
root@lnx1:~# ./showtemp
./showtemp: line 3: /sys/bus/i2c/devices/9191-0290/temp1_input: Permission denied
(standard_in) 1: parse error
 / 1000 =
So what am I doing wrong?

Regards.
Brian.
 
Old 07-18-2006, 10:00 AM   #2
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 10, Linux Mint 11
Posts: 148

Rep: Reputation: 18
When you set your a variable, put the back ticks around the command that you are running (your cat command).

Code:
a=`cat /sys/bus/i2c/devices/9191-0290/temp1_input`
 
Old 07-18-2006, 10:01 AM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Try changing it to:

a=`cat /sys/bus/i2c/devices/9191-0290/temp1_input`

The backtics makes the stdout of 'cat' seem to be a comandline component, which can be assigned to a variable. It also groups the commandline into two operations.

--- rod.
 
Old 07-18-2006, 10:03 AM   #4
zeitounator
Member
 
Registered: Aug 2003
Location: Montpellier, France, Europe, World, Solar System
Distribution: Debian Sarge, Fedora core 5 (i386 and x86_64)
Posts: 262

Rep: Reputation: 30
This line in your script:
Code:
a=cat /sys/bus/i2c/devices/9191-0290/temp1_input
basically says: put the string 'cat' in variable a and then try to run the command /sys/bus/i2c/devices/9191-0290/temp1_input. To say you want to put the result of the full command "cat /sys/bus/i2c/devices/9191-0290/temp1_input" into the variable, you need to surround it by backticks like that:
Code:
a=`cat /sys/bus/i2c/devices/9191-0290/temp1_input`
That should get you rolling

Edited: I just saw I did not answer fast enough
 
Old 07-18-2006, 10:12 AM   #5
eldaria
LQ Newbie
 
Registered: Feb 2004
Posts: 22

Original Poster
Rep: Reputation: 15
hehe 3 people with the same answer..

And it worked, I tried the quotes but I used ' instead of `
Never knew the ` but now I know.

Thanks a lot...
 
Old 07-18-2006, 10:19 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,276

Rep: Reputation: 170Reputation: 170
or ...
Code:
perl -ne 'print $_ / 1000.0' /sys/bus/i2c/devices/9191-0290/temp1_input
 
Old 07-18-2006, 11:11 AM   #7
eldaria
LQ Newbie
 
Registered: Feb 2004
Posts: 22

Original Poster
Rep: Reputation: 15
Tried the last one, but it gives no output.

Code:
root@lnx1:~# perl -ne 'print $_ / 1000.0' /sys/bus/i2c/devices/9191-0290/temp1_input
root@lnx1:~# #
 
Old 07-18-2006, 11:20 AM   #8
spirit receiver
Member
 
Registered: May 2006
Location: Frankfurt, Germany
Distribution: SUSE 10.2
Posts: 424

Rep: Reputation: 33
I guess it should say
Code:
perl -ne 'print $_ / 1000.0' < /sys/bus/i2c/devices/9191-0290/temp1_input
 
Old 07-18-2006, 01:13 PM   #9
eldaria
LQ Newbie
 
Registered: Feb 2004
Posts: 22

Original Poster
Rep: Reputation: 15
Nope,
Same result as previous one.
But that's ok, the Bash script works.
 
Old 07-18-2006, 01:19 PM   #10
eldaria
LQ Newbie
 
Registered: Feb 2004
Posts: 22

Original Poster
Rep: Reputation: 15
Actually with a bit of experimenting I found that the following also works:

Code:
echo `cat /sys/bus/i2c/devices/9191-0290/temp1_input` / 1000 | bc
 
Old 07-19-2006, 03:39 AM   #11
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,276

Rep: Reputation: 170Reputation: 170
oh well it was worth a try (perl)
I'm on solaris here so couldn't test it, apologies.
(actually both methods should work, with and without < )
 
Old 07-20-2006, 09:59 AM   #12
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 30
The perl example did work (presumably) but perl doesn't print a newline by default, so your prompt overwrote the output:

Code:
perl -ne 'print $_ / 1000.0, "\n"' ...
should work.

The shell can also do arithmetic itself with $(( ... )). Unfortunately bash is no use in this case as it can only handle integer arithmetic but more powerful shells like zsh can do floating point too, e.g.

Code:
print $(($(</sys/bus/i2c/devices/9191-0290/temp1_input)/1000.0))
Note the use of $(<...), which is equivalent to 'cat ...' except that the shell reads the file itself rather the spawning an instance of cat, so it is somewhat more efficient.
 
Old 07-20-2006, 10:52 AM   #13
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,243
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
if you really insist doing this in pure bash you can do:

Code:
declare -i a=$(</sys/bus/i2c/devices/9191-0290/temp1_input)
echo $((a/1000)).$((a - a/1000*1000))
that should do some trick
 
Old 07-20-2006, 09:26 PM   #14
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,243
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
and still a better and simpler approach:

Code:
declare -i a=$(</sys/bus/i2c/devices/9191-0290/temp1_input)
echo "${a:0:2}.${a:2}"
 
  


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
grab FTP output in bash script bokini Linux - General 2 02-03-2006 02:11 PM
Bash script to browse directory with a custom output ? raf_iso Programming 7 01-12-2006 11:09 AM
bash script to output only differences in two files czarherr Programming 4 11-08-2004 06:25 PM
bash:output file names from shell script to vi sickboy Linux - Newbie 6 10-14-2004 03:40 AM
Bash script: add all numbers from command output wi-Z-art Programming 2 08-06-2003 09:16 AM


All times are GMT -5. The time now is 11:53 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration