在OR条件下循环不起作用。 BASH贝壳

时间:2016-11-27 18:39:02

标签: bash shell input while-loop boolean

我正在尝试在OS X终端中运行shell脚本。无论何时输入m或a。

,程序都会终止

问题是,我无法使OR语句正常工作。

#!/bin/sh
read File

while [ "$File" != "m" ] || [ "$File" != "a" ]
do
read File
done

当我做的时候

while [ "$File" != "m" ]

它完美无缺。我尝试了多种方法,例如

while test $File != "m" || test $File != "a"
while test $File != "m" -o test $File != "a"
while [ $File != "m" -o $File != "a" ]

它们似乎都不起作用。 我发布的上述代码在用户输入“m”或

时不会停止循环

1 个答案:

答案 0 :(得分:2)

我建议更换

while [ "$File" != "m" ] || [ "$File" != "a" ]

通过

while [ "$File" != "m" ] && [ "$File" != "a" ]

until [ "$File" = "m" ] || [ "$File" = "a" ]

或使用正则表达式(bash):

while [[ ! $File =~ m|a ]]

请参阅bash:help until