所以我在理解如何循环一个条件时遇到了问题。这是我的例子:
#!/bin/bash
echo " Which team do you prefer, FSU or UF?"
read -r TEAM
while [ "$TEAM" != "FSU" || "UF" ]; do
echo "That was not one of your choices. Please choose FSU or UF"
if [ "$TEAM" == "FSU" ]; then
echo "You chose the better team"
else [ "$TEAM" == "UF" ];
echo "You did NOT choose the better team"
fi
done
基本上,我正在寻找的是用户输入,如果不满足该条件,它将循环回来,直到满足正确的输入。 我究竟做错了什么?在这个例子中,如果我选择FSU或Uf之外的输入为:
,我会收到错误" ./ test.sh:line 4:[:missing`]' ./test.sh:line 4:UF:命令未找到"
但是,如果我选择FSU或UF,我会得到同样的错误。
答案 0 :(得分:1)
修正脚本:
#!/bin/bash
echo " Which team do you prefer, FSU or UF?"
read -r TEAM
while [[ "$TEAM" != "FSU" && "$TEAM" != "UF" ]]; do
echo "That was not one of your choices. Please choose FSU or UF"
read -r TEAM
done
if [[ "$TEAM" == "FSU" ]]; then
echo "You chose the better team"
else [[ "$TEAM" == "UF" ]];
echo "You did NOT choose the better team"
fi
的变化:
查看Done
位置,您需要在用户输入验证通过后退出while loop
。
您需要在输入失败后询问用户的输入。因此read -r TEAM
中的while loop
。
OR
中您的逻辑比较为AND
而不是while loop
。