带有两个字符串条件的 Bash while 循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17537908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash while loop with two string conditions
提问by Joe Andolina
I have run into a bit of a problem with my bash script. My script among other things is starting a server that takes some time to get going. In order to combat the long startup, I have put in a while loop that queries the server to see if its running yet.
我的 bash 脚本遇到了一些问题。除其他外,我的脚本正在启动一个需要一些时间才能运行的服务器。为了解决长时间启动的问题,我放入了一个 while 循环来查询服务器以查看它是否正在运行。
while [ $running -eq 0 ]; do
echo "===" $response "===";
if [ "$response" == "" ] || [ "$response" == *"404 Not Found"* ]; then
sleep 1;
response=$(curl $ip:4502/libs/granite/core/content/login.html);
else
running=1;
fi
done
When exiting the loop $response equals the "404" string. If thats the case, the thing should still be in the loop, shouldn't it? Seems my loop is exiting prematurely.
退出循环时 $response 等于“404”字符串。如果是这样的话,事情应该仍然在循环中,不是吗?似乎我的循环过早退出。
Joe
乔
回答by that other guy
[ .. ]doesn't match by glob. Use [[ .. ]]:
[ .. ]与 glob 不匹配。使用[[ .. ]]:
if [ "$response" == "" ] || [[ "$response" == *"404 Not Found"* ]]; then
回答by Joe Andolina
I am not sure why my original script was failing. I assume it has to do with the fact that I was comparing against HTML. To get my script working I wound up using the string length instead of the content. The using the following comparator has everything working swimmingly.
我不确定为什么我的原始脚本失败了。我认为这与我正在与 HTML 进行比较的事实有关。为了让我的脚本工作,我最终使用了字符串长度而不是内容。使用以下比较器可以让一切顺利进行。
if [ -z "$response" ] || [ ${#response} -lt 100 ]; then
Thanks for the help, Joe
谢谢你的帮助,乔

