0

我正在制作一个 bash 脚本,该脚本根据用户输入的内容显示成绩。但是,它返回错误的成绩,我不知道为什么。我在下面发布了代码。我也是 bash 脚本的新手。

#!/bin/bash

echo "Enter assignment mark (0 to 40): "
read assignment
echo "Enter Test1 mark (0 to 15): "
read test1
echo "Enter Test2 mark (1 to 15): "
read test2
echo "Enter Final Exam mark (1 to 30): "
read final

(( sum = ( $assignment / 40 * 40 ) + (  $test1 / 15 *15 ) + ( $test2 / 15 * 15 ) + ( $final / 30 * 30 ) ))

if [ $sum -ge "90" ] && [ $sum -le "100" ]
then
echo "Your final grade is an A+."
elif [ $sum -ge "85" ] && [ $sum -le "89" ]
then
echo "Your final grade is a A."
elif [ $sum -ge "80" ] && [ $sum -le "84" ]
then
echo "Your final grade is a A-."
elif [ $sum -ge "77" ] && [ $sum -le "79" ]
then
echo "Your final grade is a B+."
elif [ $sum -ge "73" ] && [ $sum -le "76" ]
then
echo "Your final grade is a B."
elif [ $sum -ge "70" ] && [ $sum -le "72" ]
then
echo "Your final grade is a B-."
elif [ $sum -ge "67" ] && [ $sum -le "69" ]
then
echo "Your final grade is a C+."
elif [ $sum -ge "63" ] && [ $sum -le "66" ]
then
echo "Your final grade is a C."
elif [ $sum -ge "60" ] && [ $sum -le "62" ]
then
echo "Your final grade is a C-."
elif [ $sum -ge "57" ] && [ $sum -le "59" ]
then
echo "Your final grade is a D+."
elif [ $sum -ge "53" ] && [ $sum -le "56" ]
then
echo "Your final grade is an D."
elif [ $sum -ge "50" ] && [ $sum -le "52" ]
then
echo "Your final grade is an D-."
elif [ $sum -le "49" ] 
then
echo "Your final grade is an F. Sorry!"
fi
4

1 回答 1

1

我不明白这一行:

(( sum = ( $assignment / 40 * 40 ) + (  $test1 / 15 *15 ) + ( $test2 / 15 * 15 ) + ( $final / 30 * 30 ) ))

除以和乘以相同的数字似乎没有用。为什么不改成

(( sum =  $assignment + $test1 + $test2 + $final ))

编辑:此代码有效。享受 ;-)

#!/bin/bash

echo "Enter assignment mark (0 to 40): "
read assignment
echo "Enter Test1 mark (0 to 15): "
read test1
echo "Enter Test2 mark (1 to 15): "
read test2
echo "Enter Final Exam mark (1 to 30): "
read final

sum=$(( assignment  +  test1  +  test2 + final  ))

if [[ $sum -ge 90 ]] && [[ $sum -le 100 ]]
then
echo Your final grade is an A+.
elif [[ $sum -ge 85 ]] && [[ $sum -le 89 ]]
then
echo Your final grade is a A.
elif [[ $sum -ge 80 ]] && [[ $sum -le 84 ]]
then
echo Your final grade is a A-.
elif [[ $sum -ge 77 ]] && [[ $sum -le 79 ]]
then
echo Your final grade is a B+.
elif [[ $sum -ge 73 ]] && [[ $sum -le 76 ]]
then
echo Your final grade is a B.
elif [[ $sum -ge 70 ]] && [[ $sum -le 72 ]]
then
echo Your final grade is a B-.
elif [[ $sum -ge 67 ]] && [[ $sum -le 69 ]]
then
echo Your final grade is a C+.
elif [[ $sum -ge 63 ]] && [[ $sum -le 66 ]]
then
echo Your final grade is a C.
elif [[ $sum -ge 60 ]] && [[ $sum -le 62 ]]
then
echo Your final grade is a C-.
elif [[ $sum -ge 57 ]] && [[ $sum -le 59 ]]
then
echo Your final grade is a D+.
elif [[ $sum -ge 53 ]] && [[ $sum -le 56 ]]
then
echo Your final grade is an D.
elif [[ $sum -ge 50 ]] && [[ $sum -le 52 ]]
then
echo Your final grade is an D-.
elif [[ $sum -le 49 ]] 
then
echo Your final grade is an F. Sorry!
fi
于 2013-03-26T15:25:59.170 回答