LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 11-11-2008, 03:42 PM   #1
Hiperi0n
LQ Newbie
 
Registered: Jun 2008
Distribution: archlinux,ubuntu
Posts: 6

Rep: Reputation: 0
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!
 
Old 11-11-2008, 03:54 PM   #2
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,094
Blog Entries: 2

Rep: Reputation: 111Reputation: 111
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`
 
Old 11-11-2008, 04:08 PM   #3
Hiperi0n
LQ Newbie
 
Registered: Jun 2008
Distribution: archlinux,ubuntu
Posts: 6

Original Poster
Rep: Reputation: 0
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...
 
Old 11-11-2008, 05:34 PM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,289

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
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.
 
Old 11-11-2008, 05:41 PM   #5
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,094
Blog Entries: 2

Rep: Reputation: 111Reputation: 111
/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
 
Old 11-11-2008, 05:44 PM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,289

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
iirc, bc is pretty much part of the std install, I'd be surprised if it wasn't avail... hmmm embedded system maybe??
 
Old 11-11-2008, 06:16 PM   #7
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,094
Blog Entries: 2

Rep: Reputation: 111Reputation: 111
true but for embedded you could always go with dc instead (busybox has it implemented)
 
Old 11-12-2008, 03:11 AM   #8
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
You can also do it with awk if you have it installed.
 
Old 11-12-2008, 03:33 AM   #9
Hiperi0n
LQ Newbie
 
Registered: Jun 2008
Distribution: archlinux,ubuntu
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by chrism01 View Post
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 View Post
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.
 
Old 11-12-2008, 03:51 AM   #10
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
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) }'
 
Old 11-12-2008, 04:27 AM   #11
Hiperi0n
LQ Newbie
 
Registered: Jun 2008
Distribution: archlinux,ubuntu
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by H_TeXMeX_H View Post
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
 
Old 11-12-2008, 05:25 AM   #12
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
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.
 
Old 11-14-2008, 08:10 AM   #13
gmbastos
LQ Newbie
 
Registered: Apr 2007
Location: Brazil
Distribution: Slackware
Posts: 21

Rep: Reputation: 15
Thumbs up 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!

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!
 
  


Reply

Tags
bash, division, float, integer


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
Simple bash script please help! Cheloo Linux - General 3 07-13-2008 03:54 PM
Simple Bash function mijohnst Programming 13 10-23-2005 12:40 PM
Simple bash script Soulful93 Programming 1 06-04-2005 07:26 PM
Is there an integrated simple tool for calculation dragomir Linux - Newbie 1 02-26-2004 11:10 AM
simple bash q adam_boz Linux From Scratch 1 06-08-2003 11:22 AM


All times are GMT -5. The time now is 06:08 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