0

I have a for loop, within loop, there is while loop, the problem is when the while loop executed, the next for loop never executed.

sync[0]='/home/lzyy/tmp,12.34.56.78,test'
sync[1]='/home/lzyy/tmp,23.45.67.89,test'

for item in ${sync[@]}; do

echo $item #only output sync[0]
dir=`echo $item | awk -F"," '{print $1}'`
host=`echo $item | awk -F"," '{print $2}'`
module=`echo $item | awk -F"," '{print $3}'`

inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format  '%T %w%f %e' \
 --event CLOSE_WRITE,create,move,delete  $dir | while read  date time file event
    do
        echo 
        # do something, 
    done
done

what I can find out to solve this problem is async function, or save the while loop in a separate file, and executed background.

1 Answer 1

4

Your inotifywait ... | while read ...; do ... done is in fact a single command (single pipeline) in bash, so you can safely add & after it to make it run in background. Basically, it would look like that:


inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format  '%T %w%f %e' \
 --event CLOSE_WRITE,create,move,delete  $dir | while read  date time file event
    do
        echo 
        # do something, 
    done &
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.