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

问题描述 投票:0回答: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=$ ]

我的做法是错的吗?

linux bash numbers average
5个回答
5
投票

仍然不确定你想要成为什么样的人。但我认为你可以循环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

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
投票
 #!/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

0
投票

与 HackerRank 上的计算平均值问题类似,这里是代码的清理和优化版本:

# Read the count of numbers
read count

# Initialize sum to 0
sum=0

# Loop to read each number and add to sum
for (( i=1; i<=count; i++ )); do
    read num
    sum=$((sum + num))
done

# Calculate and print the average with 3 decimal places
printf "%.3f\n" $(echo "$sum / $count" | bc -l)
© www.soinside.com 2019 - 2024. All rights reserved.