Linux Bash-计算输入的数字的平均值

Linux Bash-计算输入的数字的平均值,linux,bash,numbers,average,Linux,Bash,Numbers,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=$ ] 我做错了吗?还是不确定你想成为什么样的人。但我想你可以循环3次。然后,每次迭代都会得到一组数字,并将它们相加,并跟踪您拥

需要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=$ ]

我做错了吗?

还是不确定你想成为什么样的人。但我想你可以循环3次。然后,每次迭代都会得到一组数字,并将它们相加,并跟踪您拥有的数量。下面是这样的。(注意,$numbers和$sum自动初始化为0)

示例输出在哪里

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

如果我理解正确,以下代码将按您的要求执行:

#!/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
注意-我将
count
作为一个单独的变量保留,以便您可以轻松地扩展此代码

使用
bc
允许浮点运算(不是bash内置的)<代码>比例=2表示“两个有效数字”

样本运行:

Enter three numbers:
3 4 5
The mean of these  3  values is  4.00
测试值:

    sum=200232320
    total=300123123
基本获取平均值和获取百分比:

    avg=$(echo "$sum / $total" | bc -l)
    avg=$(echo "$avg*100" | bc -l)
    printf "Average input = %.2f\n" $avg
    result=$(echo "scale=6; 1.0 * $sum / $total*100" | bc -l)
    printf "Average input = %.2f\n" $result
    result=$(echo "scale=6; 1.0 * $sum / $total*100" | bc -l) 
    printf "Average input = %.2f\n" $result
基本取平均值并获取容错百分比:

    # -- 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
    # -- 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
基本获取平均值和获取百分比:

    avg=$(echo "$sum / $total" | bc -l)
    avg=$(echo "$avg*100" | bc -l)
    printf "Average input = %.2f\n" $avg
    result=$(echo "scale=6; 1.0 * $sum / $total*100" | bc -l)
    printf "Average input = %.2f\n" $result
    result=$(echo "scale=6; 1.0 * $sum / $total*100" | bc -l) 
    printf "Average input = %.2f\n" $result
基本获取平均值和获取百分比:

    avg=$(echo "$sum / $total" | bc -l)
    avg=$(echo "$avg*100" | bc -l)
    printf "Average input = %.2f\n" $avg
    result=$(echo "scale=6; 1.0 * $sum / $total*100" | bc -l)
    printf "Average input = %.2f\n" $result
    result=$(echo "scale=6; 1.0 * $sum / $total*100" | bc -l) 
    printf "Average input = %.2f\n" $result
基本取平均值并获取百分比(带公差:

    # -- 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
    # -- 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
输出:

    ./test.sh 
    Average input = 66.72
    Average input = 66.72
    Average input = 66.72
    Average input = 66.72

你为什么要问?它不起作用吗?你使用的是什么输入,你的结果是什么?我不确定从这里走到哪里。脚本应该询问用户数量,但之后我不确定如何计算平均值。你阅读的第一个数字的目标是什么?它只是数字列表中的第一个吗?你希望用户输入吗r在数字之间返回?或者CR是否表示输入结束?请澄清您希望输入的方式,以便我们可以开始提供帮助。我希望脚本要求用户输入任意3组数字a、b、c,并使用if逻辑计算这3组数字的平均值([a+b+c/3])并显示答案。仅此而已。请原谅我正在学习Unix,这本书没有任何用处。您想要三个数字,还是三组数字?如果OP确实想要三组,那么您的代码非常好。如果是“三组数字”那就太过分了。不管怎样你都能得到我的选票。谢谢你的帮助。这正是我所需要的。非常好用,谢谢!@user3089320你应该点击这个答案旁边的小复选标记“接受”它,因为这是你喜欢的。@MikeQ不认为这个问题有必要写出每一个可能的错误用户输入,但补充了这一点和一个数字输入检查。Floris感谢你继续支持我,试图帮助我,非常感谢,但是BroSlow更好地理解了我的要求-我希望没有任何不愉快的感觉!没有任何错误你看我把他的答案投了高票。我只是不明白你在说什么——他显然明白了。