ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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, 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.
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
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.
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.
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!
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.