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" Is bc necessary? doesn't bash have any built-in alternatives? I prefer not having to install an external program (bc) Thanks! |
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` |
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...
|
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. |
/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 |
iirc, bc is pretty much part of the std install, I'd be surprised if it wasn't avail... hmmm embedded system maybe??
|
true but for embedded you could always go with dc instead (busybox has it implemented)
|
You can also do it with awk if you have it installed.
|
Quote:
Quote:
|
What about awk, does it have it ? If so you can use it to do these, for example:
Quote:
|
Quote:
|
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.
|
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:27 PM. |