bash async function
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.
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 &
精彩评论