Bash - How to make a loop only once?
When the files in a directory count
change I need to calculate total count
, and if total count
between 50 and 100 I need to run a script with input N
- which only takes 1 sec to run.
The problem is that when total count
increases every time from 50 to 100, the script is executed every time. Is there a way to stop the loop / script from running a second time?
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
source to share
I'm not sure why you have two "made" statements.
I am having a hard time following this issue. Is this what you want? Each "if" will only be executed once
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
Adjust the code for the desired behavior.
source to share