bash bash脚本来计算几个整数值的平均值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12979174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bash script to calculate average of several integer values
提问by demet8
I need to write a bash script that adds the current values and returns the average. When I run the script ./averageI get the error message: missing}. I am not sure why the script isn't printing the average to screen when I run it.
我需要编写一个 bash 脚本来添加当前值并返回平均值。当我运行脚本时,./average我收到错误消息:missing}. 我不确定为什么脚本在我运行时没有将平均值打印到屏幕上。
Here's what I have written thus far:
这是我到目前为止所写的内容:
#! /bin/csh
for var in "${sum[store1=100, store2= 75, store3= 74, store4= 100, store5= 100])}"
do
total= $sum(store1+store2+ store3+store4+store5))
echo $sum / 5
done
回答by sampson-chen
Fixed: (Updated)
固定:(更新)
#/bin/bash
total=0
list=(100 75 74 100 100)
for var in "${list[@]}"
do
total=$((total + var))
done
average=$((total/5))
echo $average

