tags:

views:

56

answers:

4

I have a shell programe, a little part of it can be seen as following:

count=1000
total=100000
percent=`expr $count/$total`

it cannot produce the division result, in the result file, only 1000/100000 was shown.

Any help? Many thanks.~

+3  A: 

You should have spaces between values to be divided and / operator like here:

count=1000
total=100000
percent=`expr $count / $total`
#                   ^ ^ - those are important
Kamil Szot
many thanks for your answer.
MaiTiano
+4  A: 

You need to add spaces before and after '/' sign:

percent=`expr $count / $total`

But it's an integer division. So you either need to multiply $count by 100 first or use something like 'bc'.

stask
+1  A: 

better use bc:

percent=$(echo "scale=2; $count/$total" | bc)
Otto Allmendinger
When I using bc as you advised, I got the following error info, what else should I add into this command? why the terminal recognize the produced .89 number as a new command? Many thanks!!./loop_count_reference_frame_no.sh: line 24: .89: command not found
MaiTiano
I got it, there should no space between = and following expression.many thanks!
MaiTiano
my bad - fixed it
Otto Allmendinger
A: 

use gawk

count=1000
total=100000
result=$(gawk -v c=$count -v t=$total 'BEGIN{print c/t }')
echo "result is $result"
ghostdog74