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.~
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.~
You should have spaces between values to be divided and / operator like here:
count=1000
total=100000
percent=`expr $count / $total`
# ^ ^ - those are important
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'.
better use bc:
percent=$(echo "scale=2; $count/$total" | bc)
use gawk
count=1000
total=100000
result=$(gawk -v c=$count -v t=$total 'BEGIN{print c/t }')
echo "result is $result"