bash는 파일이 수정되었는지 지속적으로 확인합니다.

bash는 파일이 수정되었는지 지속적으로 확인합니다.

스크립트에서 무언가를 수행하는 데 사용하고 변경 사항이 있을 때마다 실제로 경고음을 울리는 file1이라는 파일이 있습니다. 어떻게 해야 하나요?

답변1

이미 inotify-tools설치했다면(적어도 데비안의 패키지 이름) 다음을 수행할 수 있습니다:

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

이는 "filename"이라는 파일에서 "수정" 이벤트가 발생할 때까지 기다립니다. 이런 일이 발생하면 inotifywait명령 출력 filename MODIFY(/dev/null로 출력을 전송하여 삭제)이 종료되고 이로 인해 루프 본문이 입력됩니다.

더 많은 가능성을 보려면 맨페이지를 읽어보세요 inotifywait.

답변2

inotifywait 없이 cron 작업과 함께 이 작은 스크립트를 사용할 수 있습니다(1분마다).

#!/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

관련 정보