LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   simple arithmetic in bash (https://www.linuxquestions.org/questions/linux-newbie-8/simple-arithmetic-in-bash-301581/)

gfrair 03-14-2005 03:40 PM

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,

ahh 03-14-2005 03:58 PM

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.

susefan 03-14-2005 04:04 PM

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.

Boow 03-14-2005 04:15 PM

yea expr works as the poster above said

gfrair 03-14-2005 04:23 PM

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.

tredegar 03-15-2005 03:47 AM

Maybe you should look at the bc command. You may need to install it - it did not come automatically installed with Mandrake.

enemorales 03-15-2005 12:13 PM

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...

gfrair 03-15-2005 06:43 PM

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,

ahh 03-16-2005 08:50 AM

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.

gfrair 03-16-2005 02:09 PM

Thanks very much,

This should prove to be handy in the future.


All times are GMT -5. The time now is 03:23 AM.