【发布时间】:2017-04-17 11:43:14
【问题描述】:
所以我在理解如何循环条件时遇到了问题。这是我的例子:
#!/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:第 4 行:[:缺少 `]' ./test.sh: 第 4 行: UF: command not found"
但是,如果我选择 FSU 或 UF,我会得到同样的错误。
【问题讨论】:
-
[ "$TEAM" != "FSU" || "UF" ]不是有效的语法。您必须将$TEAM与每个可能的字符串进行比较 -
主要问题是 while 行应该是
while [ "$TEAM" != "FSU" || "$TEAM" != "UF" ]; do。除此之外,您在错误的地方终止了循环 (done)。 -
第三,看this page,你的
if子句的结果似乎也倒退了。
标签: bash if-statement while-loop