inotifywait 的 Bash 脚本 - 如何将删除写入日志文件和 cp close_writes?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13427656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:48:45  来源:igfitidea点击:

Bash script for inotifywait - How to write deletes to log file, and cp close_writes?

bashinotify

提问by Buttle Butkus

I have this bash script:

我有这个 bash 脚本:

#!/bin/bash


inotifywait -m -e close_write --exclude '\*.sw??$' . |
#adding --format %f does not work for some reason
while read dir ev file; do
        cp ./"$file" zinot/"$file"
done
~

Now, how would I have it do the same thing but also handle deletes by writing the filenames to a log file? Something like?

现在,我如何让它做同样的事情,但也通过将文件名写入日志文件来处理删除?就像是?

#!/bin/bash


inotifywait -m -e close_write --exclude '\*.sw??$' . |
#adding --format %f does not work for some reason
while read dir ev file; do
        # if DELETE, append $file to /inotify.log
        # else
        cp ./"$file" zinot/"$file"
done
~

EDIT:

编辑:

By looking at the messages generated, I found that inotifywait generates CLOSE_WRITE,CLOSEwhenever a file is closed. So that is what I'm now checking in my code. I tried also checking for DELETE, but for some reason that section of the code is not working. Check it out:

通过查看生成的消息,我发现 inotifywait 会CLOSE_WRITE,CLOSE在文件关闭时生成。这就是我现在检查我的代码的内容。我也尝试检查DELETE,但由于某种原因,该部分代码不起作用。一探究竟:

#!/bin/bash

fromdir=/path/to/directory/
inotifywait -m -e close_write,delete --exclude '\*.sw??$' "$fromdir" |
while read dir ev file; do
        if [ "$ev" == 'CLOSE_WRITE,CLOSE' ]
        then
                # copy entire file to /root/zinot/ - WORKS!
                cp "$fromdir""$file" /root/zinot/"$file"
        elif [ "$ev" == 'DELETE' ]
        then
                # trying this without echo does not work, but with echo it does!
                echo "$file" >> /root/zinot.txt
        else
                # never saw this error message pop up, which makes sense.
                echo Could not perform action on "$ev"
        fi

done

In the dir, I do touch zzzhey.txt. File is copied. I do vim zzzhey.txtand file changes are copied. I do rm zzzhey.txtand the filename is added to my log file zinot.txt. Awesome!

在目录中,我做touch zzzhey.txt. 文件被复制。我这样做vim zzzhey.txt并复制了文件更改。我这样做rm zzzhey.txt了,文件名被添加到我的日志文件中zinot.txt。惊人的!

回答by Ansgar Wiechers

You need to add -e deleteto your monitor, otherwise DELETEevents won't be passed to the loop. Then add a conditional to the loop that handles the events. Something like this should do:

您需要添加-e delete到您的监视器,否则DELETE事件将不会传递到循环。然后向处理事件的循环添加条件。这样的事情应该做:

#!/bin/bash

inotifywait -m -e delete -e close_write --exclude '\*.sw??$' . |
while read dir ev file; do
  if [ "$ev" = "DELETE" ]; then
    echo "$file" >> /inotify.log
  else
    cp ./"$file" zinot/"$file"
  fi
done