./txtdb: 第 9 行: [: =: 需要一元运算符

./txtdb: 第 9 行: [: =: 需要一元运算符
echo "Choose from the following options"
echo "1- Display full Database"
echo "2 - exit program"
read usrChoice
if [ $usrChoice  -eq 1 ] ; then
        cat energydrink.txt
elif [ $userChoice -eq  2 ]
then
        echo "you chose the option 2"
else
        echo "Please choose a viable command"
fi

我不明白为什么我不断收到此错误。如果我输入 1,该脚本可以工作,但如果我输入 2,我会收到此错误

./txtdb: 第 9 行: [: =: 需要一元运算符 请选择一个可行的命令

答案1

引用你的变量。

未加引号且为空变量

[ $UserChoice -eq 1 ]
-bash: [: -eq: unary operator expected

带引号但变量为空

[ "$UserChoice" -eq 1 ]
-bash: [: : integer expression expected

相关内容