我的Bash脚本工作不正常

时间:2013-03-26 15:16:58

标签: linux bash ubuntu

我正在制作一个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

1 个答案:

答案 0 :(得分: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