bash异步函数

bash异步函数,bash,Bash,我有一个for循环,在循环中,有while循环,问题是while循环何时执行,下一个for循环从未执行 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

我有一个for循环,在循环中,有while循环,问题是while循环何时执行,下一个for循环从未执行

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

解决这个问题的方法是使用异步函数,或者将while循环保存在一个单独的文件中,并在后台执行。

您的
inotifywait…|读。。。;做done
实际上是bash中的单个命令(单个管道),因此您可以安全地在其后面添加
&
,使其在后台运行。基本上,它看起来是这样的:


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 &

你的
inotifywait…|读。。。;做done
实际上是bash中的单个命令(单个管道),因此您可以安全地在其后面添加
&
,使其在后台运行。基本上,它看起来是这样的:


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 &