echo "Enter a grade"
read grade
if test $grade="A"
then
basic=6000
elif test $grade="B"
then
basic=5000
else
basic=4000
fi
echo "Your basic is $basic"
当我在任何等级的终端中执行此代码时,它总是返回“你的基本是6000”。这段代码中的错误是什么?
答案 0 :(得分:0)
#!/bin/bash
echo "Enter a grade"
# Read single char
read -n 1 grade
echo $grade
if [ "$grade" == 'A' ]; then
basic=6000
elif [ "$grade" == 'B' ]; then
basic=5000
else
basic=4000
fi
echo "Your basic is $basic"
答案 1 :(得分:0)
你需要在=。
周围加一个空格就像现在一样,你只需给测试命令一个参数,即$ grade =“A”,因为空格分隔了参数。如果在=附近放置空格,那么test将有3个参数,即(扩展的)$ grade,=和“A”。
因此:
echo "Enter a grade"
read grade
if test $grade = "A"
then
basic=6000
elif test $grade = "B"
then
basic=5000
else
basic=4000
fi
echo "Your basic is $basic"