2

我写了一个bash脚本来监控特定目录“/root/secondfolder/”,脚本如下:

#!/bin/sh

while inotifywait -mr -e close_write "/root/secondfolder/"
do
    echo "close_write"
done

当我在“/root/secondfolder/”中创建一个名为“fourth.txt”的文件并向其写入内容、保存并关闭它时,它会输出以下内容,但不会回显“close_write”:

/root/secondfolder/ CLOSE_WRITE,CLOSE fourth.txt

有人能指出我正确的方向吗?

4

2 回答 2

6

你离解决方案不远了。如果要inotifywaitwhile语句中使用,则不应使用-m选项。有了这个选项inotifywait,永远不会结束,因为它是monitor选项。所以你永远不会进入while.

这应该工作:

#!/bin/sh

while inotifywait -r -e close_write "/root/secondfolder/"
do
    echo "close_write"
done
于 2014-07-04T07:35:06.100 回答
4

事实证明,我所要做的就是将命令传递到一个 while 循环中:

!/bin/sh

inotifywait -mqr -e close_write "/root/secondfolder/" | while read line
do
echo "close_write"
done
于 2014-07-07T03:30:16.830 回答