gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 11:13:25 26 4
gpt4 key购买 nike

每当 count 目录中的文件发生变化时,我需要计算 total count,如果 total count 在 50 到 100 之间,我需要运行带有输入 N 的脚本 - 只需 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

最佳答案

我不确定您为什么有两个“完成”语句。

我很难理解这个问题。这是你想要的吗?每个“if”只会执行一次

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

根据您想要的行为调整代码。

关于linux - Bash - 如何让循环只运行一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30768585/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com