LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Simple bash calculation (http://www.linuxquestions.org/questions/programming-9/simple-bash-calculation-682695/)

Hiperi0n 11-11-2008 04:42 PM

Simple bash calculation
 
Hi all. I am going really crazy trying to do some math in a very simple bash script. I just want to calculate how much battery I have left, so I have 2 variables, charge_full and charge_now, to whom I have correctly assigned their values.

My problem comes when I try to get a percentage of the remaining power. I have tried many things but I can't get a division of both values.

How can I do this? so far:

Code:

charge_percent="$charge_now/$charge_full"
percent=`$charge_percent | bc -l`

Nothing happens, I don't know how to do this. It would be great if I could directly store the percentage in the charge_percent variable (and explained me the whole process), but I am totally messed with the variables, the piping to bc...

Is bc necessary? doesn't bash have any built-in alternatives? I prefer not having to install an external program (bc)

Thanks!

estabroo 11-11-2008 04:54 PM

you might need to echo instead of just putting $charge_percent cause it'll try and treat that as a command.

percent=`echo "$charge_percent" | bc -l`

Hiperi0n 11-11-2008 05:08 PM

Thanks, of hundreds of combinations I tried, that is the one I missed. Anyway, could I do floating point math without bc? Imagine I have no bc installed...

chrism01 11-11-2008 06:34 PM

No, shells only do integer arithmetic internally. Of course, do you REALLY need to know the value after the decimal point?
Alternately, use the old dodge of multiplying everything by eg 100 before you start (used in financial calcs).
Or use another lang eg Perl.

estabroo 11-11-2008 06:41 PM

/me finds it impossible to imagine a world without bc ;)

Sorry I have no idea what math bash can do, I always use bc for that kind of stuff

chrism01 11-11-2008 06:44 PM

iirc, bc is pretty much part of the std install, I'd be surprised if it wasn't avail... hmmm embedded system maybe??

estabroo 11-11-2008 07:16 PM

true but for embedded you could always go with dc instead (busybox has it implemented)

H_TeXMeX_H 11-12-2008 04:11 AM

You can also do it with awk if you have it installed.

Hiperi0n 11-12-2008 04:33 AM

Quote:

Originally Posted by chrism01 (Post 3338830)
No, shells only do integer arithmetic internally. Of course, do you REALLY need to know the value after the decimal point?
Alternately, use the old dodge of multiplying everything by eg 100 before you start (used in financial calcs).
Or use another lang eg Perl.

That is a simple, yet wonderful solution. Who needs floating point if you can put a few zeros to the quotient and voilá! In this exact case I need to know the fraction, as we are talking about power supply's mAh. Perl would mean, well, installing perl ^_^

Quote:

Originally Posted by chrism01 (Post 3338830)
iirc, bc is pretty much part of the std install, I'd be surprised if it wasn't avail... hmmm embedded system maybe??

Fresh archlinux install. I have also tried in a standard cygwin and doesn't have it either.

H_TeXMeX_H 11-12-2008 04:51 AM

What about awk, does it have it ? If so you can use it to do these, for example:
Quote:

echo 3.14158 2.348 | awk '{ printf("%f\n", $1/$2) }'

Hiperi0n 11-12-2008 05:27 AM

Quote:

Originally Posted by H_TeXMeX_H (Post 3339238)
What about awk, does it have it ? If so you can use it to do these, for example:

That would also work. However, I am trying to master BASH first of all, and as it appears, I still have a long journey :)

H_TeXMeX_H 11-12-2008 06:25 AM

But, awk is just a command, a very useful command. True it can also be considered a language in and of itself, but it's included in almost all bash tutorials.

gmbastos 11-14-2008 09:10 AM

Fake floating point
 
Right, Hiperion. Long road ahead. But sorry, buddy: Pure Bash has only integer arithmetics. :(

And I agree with H_TeXMeX_H: AWK is a great tool! :D

The solutions pointed out earlier (BC, DC, PERL and also AWK) are surely very common to a number of distributions and are frequently indicated by several Bash forum posts, articles and tutorials.

Please note that a good shell scripter must also know how to use a comprehensive (and coherent) set of shell tools. :)

Pick the one you find most sympathetic and learn it. It is worth the heat. :)

That multiplication workaround chrism01 suggested, however, is always useful for simulating floating point arithmetics: just multiply by an appropriately large number. I mean: provided you want just one decimal digit, you could multiply your numerator by 1000 (one thousand), evaluate the integer division and put a point (or whatever decimal separator your locale defines) right before the last digit.

I hope the following example clarify my point:

[CODE]
#!/bin/sh
result=$((30*1000/60));
integpart=`echo $result | cut -b -2`
floatpart=`echo $result | cut -b 3`
echo $integpart.$floatpart %
[\CODE]

Good learning. Have fun! :D


All times are GMT -5. The time now is 05:58 AM.