linux - Shell scripting, finding average of loop results -


i made loop find results of calculating each number (1 10 mod 5) + 2

for (( = 0; <= 10; i++))      calculate=$(( % 5 + 2 ))     echo "($i % 5) + 2 = " $calculate done average=$(($calculate/10)) echo $average`  

my problem fixing code can take results of loop , find average of them returning 0 average

you have keep full total - ($calculate/10) last iteration. keep running total initialized 0 before loop total = 0 ... add calculated value total in each iteration of loop total = $( $total + $calculate ) average total/10 (not calculate/10).

#!/bin/bash total=0 (( = 0; <= 10; i++))      calculate=$(( % 5 + 2 ))     total=$(( $total + $calculate ))     echo "($i % 5) + 2 = " $calculate done #average=$(($calculate/10)) average=$(($total/10)) echo $average 

Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -