在Bash中添加/平均几个变量

时间:2018-02-01 03:02:09

标签: linux bash shell variables scripting

我遇到了这个问题,我已经在晚上的大部分时间内进行了摔跤。我需要让这个脚本添加变量然后将它们平均化,并在计算后显示学生姓名,总分和平均值。

以下是我所拥有的:

#!/bin/bash

echo "================"
echo "Grade Calculator"
echo "================"

read -p "Enter first name: " $FirstName
read -p "Enter last name: " $LastName

read -p "Enter test score 1: " $ScoreOne
read -p "Enter test score 2: " $ScoreTwo
read -p "Enter test score 3: " $ScoreThree
read -p "Enter test score 4: " $ScoreFour
read -p "Enter lab score: " $Lab
Total=expr `$ScoreOne + $ScoreTwo + $ScoreThree + $ScoreFour + $Lab`

echo "Grade results. . ."
echo "Student name: $FirstName $LastName"
echo "Total points: $Total" 

任何指针都会受到赞赏。

2 个答案:

答案 0 :(得分:2)

编辑脚本本身,如果有帮助,请试试,请告诉我。

#!/bin/bash

echo "================"
echo "Grade Calculator"
echo "================"

read -p "Enter first name: " FirstName
read -p "Enter last name: " LastName

read -p "Enter test score 1: " ScoreOne
read -p "Enter test score 2: " ScoreTwo
read -p "Enter test score 3: " ScoreThree
read -p "Enter test score 4: " ScoreFour
read -p "Enter lab score: " Lab
Total=$((ScoreOn + ScoreTwo + ScoreThree + ScoreFour + Lab))

echo "Grade results. . ."
echo "Student name: $FirstName $LastName"
echo "Total points: $Total"

编辑: 如果您想使用expr,可以使用以下代码更改上面代码的total变量行:

Total=$(expr "$ScoreOne" + "$ScoreTwo" + "$ScoreThree" + "$ScoreFour")

答案 1 :(得分:0)

您消失的变量值可以通过以下方式解释:

read -p "Enter first name: " $FirstName
read -p "Enter last name: " $LastName

注意:>变量名称前的'$'不正确。分配值时不包括'$'

虽然您可以将输入转换为固定变量名称,但通过这样做,您可以将自己限制为功能有限的脚本,该脚本不能(或不少)使用您为其指定变量名称的值的数量。解决同一问题的另一种方法是简单地读取循环中的值,该循环将在用户在空行上按 Enter 时终止(不输入分数)。

由于您的问题已标记为bash,因此您只需测试输入是否为整数等级,然后将该值添加到数组中。输入完成后,您可以计算有关您喜欢的成绩的任何统计数据。 (您可以在输入时或输入完成后对值进行求和)

Bash不提供任何类型的浮点数处理,因此对于您的平均值,您需要bc(或calc)的帮助来计算您的浮点值。

将各个部分组合在一起,您可以执行以下操作。 (有很多方法可以解决这个问题)

#!/bin/bash

declare -a grades
declare -i total=0

echo "================"
echo "Grade Calculator"
echo "================"

read -p "Enter first name: " FirstName
read -p "Enter last name: " LastName

score=" "
## loop collecting grades until user presses return on empty line
printf "(Press return on empty line to end input)\n-----------------\n"
while [ -n "$score" ]; do
    read -p "Enter test score: " score
    ## validate integer and add to array
    [ "$score" -eq "$score" >/dev/null 2>&1 ] && grades+=( $score )
done

echo "Grade results. . ."
echo "Student name: $FirstName $LastName"
printf "\ngrades:\n"
for i in "${grades[@]}"; do
    printf "  %3d\n" "$i"
    total=$((total + i))
done
printf -- " -----\n  %3d  total\n\n" "$total"

## compute average with bc
printf "Average: %s\n" $(printf "scale=2; %d / %d\n" $total ${#grades[@]} | bc)

示例使用/输出

$ bash tmp/grades.sh
================
Grade Calculator
================
Enter first name: John
Enter last name: Doe
(Press return on empty line to end input)
-----------------
Enter test score: 88
Enter test score: 94
Enter test score: 92
Enter test score: 80
Enter test score: 87
Enter test score:
Grade results. . .
Student name: John Doe

grades:
   88
   94
   92
   80
   87
 -----
  441  total

Average: 88.20

通过检查用户输入的整数作为输入,如果出现错误,您的输入不会受到影响,例如

Enter test score: 88
Enter test score: uugh, messed up
Enter test score: 92

(简单地忽略非整数输入)