【发布时间】:2014-10-30 06:59:24
【问题描述】:
基本上我想做的是从 std 输入中读取一个变量并检查该变量是 1 还是 2。如果不是,我想通知用户并再次询问,我认为 while 循环是有意义的(我在 bash shell 中使用 OS X Mavericks)
这适用于单一条件:
read result
while [[ "$result" != "1" ]];
do
echo $result
echo "You must enter 1 or 2. Please try again: "
read result
done
echo "success"
但是当我尝试做一个或条件时:
while [[ "$result" != "1" ]] || [[ "$result" != "2" ]];
如果我在终端中输入 1 或 2,这与输入的 1 或 2 不匹配。我也尝试过
while [[ "$result" != "1" ] || [ "$result" != "2" ]];
会产生语法错误。
while [[ "$result" != "1" || "$result" != "2" ]];
这也不匹配 1 或 2。 有谁知道正确的语法??
【问题讨论】:
标签: bash shell while-loop osx-mavericks sh