我正在尝试将用户输入与字符串数组值进行比较,当我这样做时,当我打印它们时,两个值看起来都相同,但if语句看不到它们是相等的。有什么明显的东西我不见了。
k=0
while [ -z $arg1 ]
do
while [ $k -ne 9 ]
do
# ask question
echo "${question[k]}"
answer="${answer[k]}"
read -p "YES/NO " arg1
# check answer
if [ '$arg1' = '$answer' ]; # problem is here
then
echo "Correct!"
let "k=$k+1"
else
echo "Incorrect!"
echo "Your answer: $arg1"
echo "Correct answer: $answer"
fi
done
done
答案 0 :(得分:0)
替换:你还没有把答案和问题数组放在
中 if [ '$arg1' = '$answer' ];
with
if [ "$arg1" = "$answer" ] ;
并检查