Bash - 计算输入数字的平均值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20509541/
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 - Calculate the Average of Numbers Inputted
提问by user3089320
Need help with Linux Bash script. Essentially, when run the script asks for three sets of numbers from the user and then calculates the numbers inputted and finds the average.
需要有关 Linux Bash 脚本的帮助。本质上,脚本在运行时会要求用户提供三组数字,然后计算输入的数字并找到平均值。
#!/bin/bash
echo "Enter a number: "
read a
while [ "$a" = $ ]; do
echo "Enter a second set of numbers: "
read b
b=$
if [ b=$ ]
Am I going about this wrong?
我这样做是错误的吗?
回答by Reinstate Monica Please
Still not sure what you want a to be. But I think you can just loop 3 times. Then each iteration get a set of numbers, and add them up and keep track of how many you have. So something like below. (note $numbers and $sum are initialized to 0 automatically)
仍然不确定你想要什么。但我认为你可以循环 3 次。然后每次迭代都会得到一组数字,并将它们相加并记录您拥有的数字。所以像下面这样。(注意 $numbers 和 $sum 会自动初始化为 0)
#!/bin/bash
sum=0
numbers=0
for a in {1..3}; do
read -p $'Enter a set of numbers:\n' b
for j in $b; do
[[ $j =~ ^[0-9]+$ ]] || { echo "$j is not a number" >&2 && exit 1; }
((numbers+=1)) && ((sum+=j))
done
done
((numbers==0)) && avg=0 || avg=$(echo "$sum / $numbers" | bc -l)
echo "Sum of inputs = $sum"
echo "Number of inputs = $numbers"
printf "Average input = %.2f\n" $avg
Where example output would be
示例输出在哪里
Enter a set of numbers:
1 2 3
Enter a set of numbers:
1 2 3
Enter a set of numbers:
7
Sum of inputs = 19
Number of inputs = 7
Average input = 2.71
回答by Floris
If I understood you correctly, the following code will do what you asked:
如果我理解正确,以下代码将按照您的要求执行:
#!/bin/bash
echo "Enter three numbers:"
read a b c
sum=$(($a + $b + $c))
count=3
result=$(echo "scale=2; 1.0 * $sum / $count" | bc -l)
echo "The mean of these " $count " values is " $result
Note - I left count
as a separate variable so you can easily extend this code.
注意 - 我count
作为一个单独的变量留下,因此您可以轻松扩展此代码。
The use of bc
allows floating point arithmetic (not built in to bash); scale=2
means "two significant figures".
使用bc
允许浮点运算(不是内置于 bash);scale=2
表示“两位有效数字”。
Sample run:
示例运行:
Enter three numbers:
3 4 5
The mean of these 3 values is 4.00
回答by Mike Q
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
回答by tech bie
#!/bin/bash
echo "Enter size"
read s `#reading size of the average`
i=1 `#initializing`
sum=0 `#initializing`
echo "Enter the factors"
while [ $i -le $s ]
do
read factors
sum=$((sum + factors))
i=$((i + 1))
done
avg=$(echo $sum / $s | bc -l)
echo "Average of factors is" $avg