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

5

需要帮助编写 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=$ ]

我这样做是不是有问题?


为什么你要问?它不起作用吗?你在使用哪些输入,你的结果是什么? - Floris
我不确定接下来该怎么做。脚本应该要求用户输入数字,但之后我不确定如何计算平均数。 - user3089320
第一个数字的目标是什么?它只是数字列表中的第一个吗?您是否希望用户在数字之间输入回车符?还是CR表示输入结束?请澄清您想要的输入方式,以便我们开始帮助。 - Floris
我希望脚本能够要求用户输入任意3组数字a、b、c,并使用if逻辑计算这3组数字的平均值([a+b+c / 3]),并显示答案。就这样。请原谅我正在学习Unix,但书对我没有用处。 - user3089320
你需要三个数字,还是三组数字? - Floris
4个回答

5

我不确定您希望什么作为变量a。但我认为您可以循环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                               

示例输出将是

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

如果 OP 确实想要“三个集合”,那么你的代码非常好。如果是“三个数字的集合”,那就有些过度了。无论哪种方式,你都得到了我的投票。 - Floris
感谢BroSlow的帮助。这正是我所需要的。非常好用且易于使用,谢谢! - user3089320
@user3089320,您应该点击此答案旁边的小复选框来“接受”它,因为这是您偏爱的答案。 - Floris
@MikeQ 不认为这个问题需要编写出每种可能的错误用户输入,但我已经添加了一个数字输入检查。 - Reinstate Monica Please

1
如果我理解正确,以下代码将实现您的要求:
#!/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); scale=2表示“两个有效数字”。
示例运行:
Enter three numbers:
3 4 5
The mean of these  3  values is  4.00

1
Floris,感谢您一直以来的支持和帮助,非常感激。然而,BroSlow更好地理解了我所要求的内容 - 希望没有伤害到您的感情! - user3089320
完全没有 - 你看到我给他的回答点了赞,我只是无法理解你所说的话 - 他显然明白了。 - Floris

0
 #!/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

0

测试数值:

    sum=200232320
    total=300123123

基本的取平均值和获取百分比:

    avg=$(echo "$sum / $total" | bc -l)
    avg=$(echo "$avg*100" | bc -l)
    printf "Average input = %.2f\n" $avg

基本的取平均值和获取百分比,具备容错能力:

    # -- 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

基本的取平均值和获取百分比:

    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

基本取平均并获取百分比(带容差):

    # -- 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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接