Bash – 如何让循环只运行一次?

每当count目录中的文件发生变化,我需要计算total count ,如果total count在50到100之间,我需要运行一个脚本inputN – 只需要1秒运行。

问题是当total count每次从50增加到100时,脚本每次都在执行。 有没有办法阻止循环/脚本第二次运行?

 while inotifywait -r -e modify $dir do line=$(grep -ho '[0-9]*' /var/count* | awk '{sum+=$1} END {print sum}') echo "********** count is $line **********" if [ $line -ge 50 ] && [ $line -lt 100 ] then echo "_____________executing if 1 _______________" export N=1 /var/test.sh fi if [ $line -ge 100 ] && [ $line -lt 150 ] then echo "_____________executing if 2 _______________" export N=2 /var/test.sh fi done 

我不知道为什么你有两个“完成”的陈述。

我在这个问题上遇到困难。 这是你想要的吗? 每个“如果”只会执行一次

 export N=0 while inotifywait -r -e modify $dir do line=$(grep -ho '[0-9]*' /var/count* | wc -l) if [ $N -lt 1 -a $line -ge 50 -a $line -lt 100 ] then export N=1 /var/test.sh elif [ $N -lt 2 -a $line -ge 100 -a $line -lt 150 ] then export N=2 /var/test.sh fi done 

调整你想要的行为的代码。