Test values:
sum=200232320
total=300123123
Basic take average and get percentage:
avg=$(echo "$sum / $total" | bc -l)
avg=$(echo "$avg*100" | bc -l)
printf "Average input = %.2f\n" $avg
Basic take average and get percentage with fault tolerance:
# -- what if sum=0 or greater than total?
if [ $sum -eq 0 ] || [ $sum -gt $total ] ;then
avg=0
else
avg=$(echo "$sum / $total" | bc -l)
avg=$(echo "$avg*100" | bc -l)
fi
printf "Average input = %.2f\n" $avg
Basic take average and get percentage:
result=$(echo "scale=6; 1.0 * $sum / $total*100" | bc -l)
printf "Average input = %.2f\n" $result
Basic take average and get percentage:
result=$(echo "scale=6; 1.0 * $sum / $total*100" | bc -l)
printf "Average input = %.2f\n" $result
Basic take average and get percentage (with tolerance:
# -- if sum is greater than total we have a problem add in 1.0 to address div by zero
[[ $sum -gt $total ]] && result=0 || result=$(echo "scale=6; 1.0 * $sum / $total*100" | bc -l)
printf "Average input = %.2f\n" $result
output:
./test.sh
Average input = 66.72
Average input = 66.72
Average input = 66.72
Average input = 66.72