简体   繁体   English

Bash while循环在处理多个测试条件时的行为不同

[英]Bash while loop behaves differently dealing with multiple test conditions

I wonder if someone could explain why a while loop treats multipe test conditions differently to an if loop. 我想知道是否有人可以解释为什么while循环将多次测试条件与if循环区别对待。 I have 2 tests that i verified came out as True and False: 我已经验证了2个测试,分别为对与错:

Bash$ test ! -n "$(find . -maxdepth 1 -name '*.xml' -print -quit)"; echo $?
0
Bash$ test ! -e "unsentData.tmp"; echo $?
1
Bash$ 

When I ANDed these 2 tests into an if statement I got an aggregate of False as expected: 当我将这2个测试与AND到if语句中时,按预期方式我得到了False的集合:

Bash$ if [ ! -n "$(find . -maxdepth 1 -name '*.xml' -print -quit)" ] && [ ! -e "unsentData.tmp" ]; then echo "True"; else echo "False"; fi
False
Bash$

Now when I put the 2 tests into a while loop I expected a sleep until both conditions were met but instead I got immediately a true result. 现在,当我将2个测试放入一个while循环中时,我希望在满足两个条件之前都可以入睡,但我立即获得了真实的结果。

Bash$ while [ ! -n "$(find . -maxdepth 1 -name '*.xml' -print -quit)" ] && [ ! -e "unsentData.tmp" ]; do sleep 1; done; echo -e "All files Exist\n$(ls /opt/pcf/mfe/unsentXmlToTSM/xmlConnection0_TSM/)"
All files Exist 
unsentData.tmp
Bash$

What am I missing here? 我在这里想念什么? I simply want to write something that waits until the 2 conditions are met before it breaks out of the loop 我只想写一些东西,等到两个条件都满足后再退出循环

A 一种

Your assumption is worng i think. 我认为您的假设很陈旧。 While does execute the code between do and done while (as long as) the condition holds true. While确实在dodone之间执行代码,而条件成立。 Your conditions combined evaluate to false, as seen in the output of your if-statement. 如if语句的输出所示,您的条件总和为false。 Thus the body of the while loop never gets executed. 因此,while循环的主体永远不会执行。 Try: 尝试:

while ! ( [ ! -n "$(find . -maxdepth 1 -name '*.xml' -print -quit)" ] && 
          [ ! -e "unsentData.tmp" ] )  
do 
    sleep 1
done 
echo -e "All files Exist\n$(ls /opt/pcf/mfe/unsentXmlToTSM/xmlConnection0_TSM/)"
while [ ! -n "$(find . -maxdepth 1 -name '*.xml' -print -quit)" ] && [ ! -e "unsentData.tmp" ]
    do sleep 1
done

==> ==>

while false
    do sleep 1
done

so the do sleep 1 didn't run at all. 所以do sleep 1根本没有运行。

A while loop executes as long as (" while ") its condition is true; 只要(“ while ”)条件成立,就执行while循环; it sounds like you want to run the loop until its condition is true. 听起来您想运行循环直到其条件为真。 bash has an until loop which does exactly this: bash有一个until做到的循环:

until [ ! -n "$(find . -maxdepth 1 -name '*.xml' -print -quit)" ] && [ ! -e "unsentData.tmp" ]; do
    sleep 1
done
echo -e "All files Exist\n$(ls /opt/pcf/mfe/unsentXmlToTSM/xmlConnection0_TSM/)"

Or you can just negate the condition (ie use "while there are files left, do..." rather than "until all files are done, do..."). 或者,您可以否定条件(例如,使用“在剩余文件时,执行...”,而不是“直到所有文件都完成,执行...”)。 In this case that just means removing the negations of the individual conditions and switching the and to an or : 在这种情况下,仅仅表示去除的各个条件否定和切换以及一种

while [ -n "$(find . -maxdepth 1 -name '*.xml' -print -quit)" ] || [ -e "unsentData.tmp" ]; do
    sleep 1
done
echo -e "All files Exist\n$(ls /opt/pcf/mfe/unsentXmlToTSM/xmlConnection0_TSM/)"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM