LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Bash Script, calculate output. (http://www.linuxquestions.org/questions/programming-9/bash-script-calculate-output-465209/)

eldaria 07-18-2006 10:50 AM

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.

Hobbletoe 07-18-2006 11:00 AM

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`

theNbomr 07-18-2006 11:01 AM

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.

zeitounator 07-18-2006 11:03 AM

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 :)

eldaria 07-18-2006 11:12 AM

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. :study:

Thanks a lot...

bigearsbilly 07-18-2006 11:19 AM

or ...
Code:

perl -ne 'print $_ / 1000.0' /sys/bus/i2c/devices/9191-0290/temp1_input

eldaria 07-18-2006 12:11 PM

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:~# #


spirit receiver 07-18-2006 12:20 PM

I guess it should say
Code:

perl -ne 'print $_ / 1000.0' < /sys/bus/i2c/devices/9191-0290/temp1_input

eldaria 07-18-2006 02:13 PM

Nope,
Same result as previous one.
But that's ok, the Bash script works. :)

eldaria 07-18-2006 02:19 PM

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

bigearsbilly 07-19-2006 04:39 AM

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 < )

ioerror 07-20-2006 10:59 AM

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.

konsolebox 07-20-2006 11:52 AM

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 :)

konsolebox 07-20-2006 10:26 PM

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}"



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