bash不断检查文件是否被修改

bash不断检查文件是否被修改

我有一个名为 file1 的文件,我想在脚本中使用,只要其中有更改,就执行某些操作,实际上会发出蜂鸣声。我怎么做?

答案1

如果你已经inotify-tools安装了(至少是 Debian 上的包名称)时你可以执行如下操作:

while inotifywait -q -e modify filename >/dev/null; do
    echo "filename is changed"
    # do whatever else you need to do
done

这会等待名为“filename”的文件发生“modify”事件。当发生这种情况时,inotifywait命令输出filename MODIFY(我们通过将输出发送到 /dev/null 来丢弃)然后终止,这会导致进入循环体。

阅读联机帮助页以inotifywait获取更多可能性。

答案2

如果没有 inotifywait,您可以使用这个小脚本和 cron 作业(每分钟左右):

#!/usr/bin/env bash
#
# Provides      : Check if a file is changed
# 
# Limitations   : none
# Options       : none
# Requirements  : bash, md5sum, cut
# 
# Modified      : 11|07|2014
# Author        : ItsMe
# Reply to      : n/a in public
#
# Editor        : joe
#
#####################################
#
# OK - lets work
#

# what file do we want to monitor?
# I did not include commandline options
# but its easy to catch a command line option
# and replace the defaul given here
file=/foo/bar/nattebums/bla.txt

# path to file's saved md5sum
# I did not spend much effort in naming this file
# if you ahve to test multiple files
# so just use a commandline option and use the given
# file name like: filename=$(basename "$file")
fingerprintfile=/tmp/.bla.md5savefile

# does the file exist?
if [ ! -f $file ]
    then
        echo "ERROR: $file does not exist - aborting"
    exit 1
fi

# create the md5sum from the file to check
filemd5=`md5sum $file | cut -d " " -f1`

# check the md5 and
# show an error when we check an empty file
if [ -z $filemd5 ]
    then
        echo "The file is empty - aborting"
        exit 1
    else
        # pass silent
        :
fi

# do we have allready an saved fingerprint of this file?
if [ -f $fingerprintfile ]
    then
        # yup - get the saved md5
        savedmd5=`cat $fingerprintfile`

        # check again if its empty
        if [ -z $savedmd5 ]
            then
                echo "The file is empty - aborting"
                exit 1
        fi

        #compare the saved md5 with the one we have now
        if [ "$savedmd5" = "$filemd5" ]
            then
                # pass silent
                :
            else
                echo "File has been changed"

                # this does an beep on your pc speaker (probably)
                # you get this character when you do:
                # CTRL+V CTRL+G
                # this is a bit creepy so you can use the 'beep' command
                # of your distro
                # or run some command you want to
                echo 
        fi

fi

# save the current md5
# sure you don't have to do this when the file hasn't changed
# but you know I'm lazy and it works...
echo $filemd5 > $fingerprintfile

答案3

来寻找 MacOS 上的一句台词。决定如下。编译并添加这个工具走向我的道路。这花费了不到 30 秒的时间。

$ git clone [email protected]:sschober/kqwait.git
$ cd kqwait
$ make
$ mv kqwait ~/bin
$ chmod +x ~/bin/kqwait

接下来,我进入我想要观看的目录。在这种情况下,我希望查看 Markdown 文件的更改,如果更改,则发出make.

$ while true; do kqwait doc/my_file.md; make; done

就是这样。

答案4

你可以试试entr命令行工具,例如

$ ls file1 | entr beep

相关内容