LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-14-2005, 04:40 PM   #1
gfrair
LQ Newbie
 
Registered: Jan 2005
Posts: 20

Rep: Reputation: 0
simple arithmetic in bash


I am trying to do a relatively easy arithmetic operation and bash is making it literally impossible. I have been searching google and tutorials for nearly 3 hours and about ready to just give up and say f*ck it.

This is what I'm trying to do:

$x=309
$y=98

I am trying to perform "( y / x ) * 100" to get the percentage as a decimal.

(the answer should be 31.71%)

I have tried a thousand different combinations of things and have come up with nothing. I would like to murder the person who made the arithmetic so hard to perform in bash.

I would be permanently in debt to whoever can help me out.

Thanks,
 
Old 03-14-2005, 04:58 PM   #2
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
The nearest you will get with bash arithmatic is:
Code:
x=309
y=98
echo $((y*100/$x))
Bash only does integers, (as far as I know). So if you do 98/309 you get 0, hence you must multiply 98 by 100 first.

If you want to play around,
Code:
$(($y*100%$x))
will give you the remainder.

With a bit of work you can get decimal places from this and concantenate them to get the result.
 
Old 03-14-2005, 05:04 PM   #3
susefan
Member
 
Registered: Feb 2005
Location: California, USA
Distribution: Novell SuSE
Posts: 39

Rep: Reputation: 15
I'm not sure if bash (or any other shell) can do arithmetic directly,
but it is certainly possible to give the problem to another process,
and return the answer into bash:

x=309.0
y=98.0
z=`python -c "print $y / $x * 100"`
echo $z

Want to use something other than python?
Try Perl or expr.
 
Old 03-14-2005, 05:15 PM   #4
Boow
Member
 
Registered: Feb 2004
Distribution: Slackware 10.2
Posts: 669

Rep: Reputation: 32
yea expr works as the poster above said

Last edited by Boow; 03-14-2005 at 05:19 PM.
 
Old 03-14-2005, 05:23 PM   #5
gfrair
LQ Newbie
 
Registered: Jan 2005
Posts: 20

Original Poster
Rep: Reputation: 0
that won't work because $x and $y are already whole numbers and i can't simply add a decimal on the end because they are taken from performing a command. They represent the number of files in a certain directory.
 
Old 03-15-2005, 04:47 AM   #6
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Debian "Jessie"
Posts: 6,085

Rep: Reputation: 398Reputation: 398Reputation: 398Reputation: 398
Maybe you should look at the bc command. You may need to install it - it did not come automatically installed with Mandrake.
 
Old 03-15-2005, 01:13 PM   #7
enemorales
Member
 
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410

Rep: Reputation: 31
Well, you could do

Code:
x=$x+0.0
or calculate

Code:
z=($y+0.0)/($x+0.0)
But those will fail anyway. As ahh pointed out, bash only does integer arithmetic. Quoting http://linuxreviews.org/beginner/abs.../en/c2850.html

Quote:
Bash does not understand floating point arithmetic. It treats numbers containing a decimal point as strings.

a=1.5

let "b = $a + 1.3" # Error.
# t2.sh: let: b = 1.5 + 1.3: syntax error in expression (error token is ".5 + 1.3")

echo "b = $b" # b=1

Use bc in scripts that that need floating point calculations or math library functions.
A page I find very good for learning bach is here: http://linuxreviews.org/beginner/abs-guide

Hope this helps...

Last edited by enemorales; 03-15-2005 at 01:14 PM.
 
Old 03-15-2005, 07:43 PM   #8
gfrair
LQ Newbie
 
Registered: Jan 2005
Posts: 20

Original Poster
Rep: Reputation: 0
thanks guys,

but I just ended up multipying the numberator by 100 and said 'screw it' to the decimal, i couldn't figure it out, so I just went with rounded number.

Thanks again,
 
Old 03-16-2005, 09:50 AM   #9
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
This will give you 2 decimal places if you still interested:
Code:
#!/bin/bash

num_to_be_divided=98
num_to_divide_by=309

# to get 2 decimal places multiply num_to_be_divided by 100

new_num_to_be_divided=$(($num_to_be_divided*100))

# do the percentage calculation - not forgetting to multiply before dividing

temp_percent=$(($new_num_to_be_divided*100/$num_to_divide_by))

# get the integer value and decimal places of the real percentage

int_percent=$(($temp_percent/100))
dec_percent=$(($temp_percent%100))

# print the answer

echo $int_percent"."$dec_percent"%"

exit 0
Note that the second decimal place is not completely accurate though.

For example, anything from 31.710 to 31.719 will be 31.71 as there is no rounding up.

If it needs to be more accurate you can get three decimal places and increment the second decimal place as necessary.
 
Old 03-16-2005, 03:09 PM   #10
gfrair
LQ Newbie
 
Registered: Jan 2005
Posts: 20

Original Poster
Rep: Reputation: 0
Thanks very much,

This should prove to be handy in the future.
 
  


Reply


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 problem xushi Programming 2 04-22-2004 08:02 AM
Bash, non-integers and arithmetic causticmtl Programming 5 07-16-2003 10:15 AM
Basic Java arithmetic question chr15t0 Programming 2 07-05-2003 02:32 PM
simple bash q adam_boz Linux From Scratch 1 06-08-2003 12:22 PM
arithmetic operators in Kylix3 (C++) herbie_52 Programming 2 05-23-2003 08:36 AM


All times are GMT -5. The time now is 06:32 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration