gpt4 book ai didi

linux - 无法回显对文件所做的更改

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:11:22 25 4
gpt4 key购买 nike

我正在检查文件是否已被修改,我需要回显添加了哪些新字符串。当我为单个文件尝试此脚本时它可以工作,但是当我遍历目录中的多个文件时它无法正常工作。有什么建议吗?

#! /bin/bash

GAP=5

while :
do
FILES=/home/Desktop/*
for f in $FILES
do
len=`wc -l $f | awk '{ print $1 }'`

if [ -N $f ]; then
echo "`date`: New entries in $f:"
newlen=`wc -l $f | awk '{ print $1 }'`
newlines=`expr $newlen - $len`
tail -$newlines $f
len=$newlen
fi
sleep $GAP
done
done

最佳答案

继续评论,这是我设想的使用 inotifywait(来自 inotify-tools 包)和关联数组的原始解决方案。这里的好处是 inotifywait 将阻塞并且不会浪费资源无休止地检查每个循环迭代中每个文件的行数。我将致力于使用临时文件的解决方案,但是当您走这条路时,您会敞开心扉接受循环迭代之间发生的变化。这是第一个解决方案:

#!/bin/bash

watchdir="${1:-$PWD}"
events="-e modify -e attrib -e close_write -e create -e delete -e move"
declare -A lines

for i in "$watchdir"/*; do
[ -f "$i" ] && lines[$i]=$(wc -l <"$i")
done

while :; do ## watch for changes in chosen dir
fname="${watchdir}/$(inotifywait -q $events --format '%f' "$watchdir")"
newlc=$(wc -l <"$fname") ## get line count for changed file
if [ "${lines[$fname]}" -ne "$newlc" ]; then ## if changed, print
printf " lines chanaged : %s -> %s (%s)\n" \
"${lines[$fname]}" "$newlc" "$fname"
lines[$fname]=$newlc ## update saved line count for file
fi
done

原始测试文件.txt

$ cat dat/tmp/testfile.txt
1 1.2
2 2.2

示例使用/输出

保存在 watchdir.sh 中的脚本。启动 watchdir.sh 所以 inotifywait 正在监视 dat/tmp 目录

$ ./watchdir.sh dat/tmp

使用第二个终端,修改dat/tmp 目录中的文件

$ echo "newline" >> ~/scr/tmp/stack/dat/tmp/testfile.txt
$ echo "newline" >> ~/scr/tmp/stack/dat/tmp/testfile.txt

在单独的终端(或后台)运行的 watchdir.sh 的输出

$ ./watchdir.sh dat/tmp
lines chanaged : 2 -> 3 (dat/tmp/testfile.txt)
lines chanaged : 3 -> 4 (dat/tmp/testfile.txt)

结果 testfile.txt

$ cat dat/tmp/testfile.txt
1 1.2
2 2.2
newline
newline

第二种解决方案使用 [ -N file ]

这是第二个解决方案,它更接近您的第一次尝试。这是接近解决方案的一种不太可靠的方法(它会错过测试之间的多个更改等)。查看它,如果您有任何问题,请告诉我

#!/bin/bash

watchdir="${1:-$PWD}"
gap=5
tmpfile="$TMPDIR/watchtmp" ## temp file in system $TMPDIR (/tmp)
:>"$tmpfile"

trap 'rm $tmpfile' SIGTERM EXIT ## remove tmpfile on exit

for i in "$watchdir"/*; do ## populate tmpfile with line counts
[ -f "$i" ] && echo "$i,$(wc -l <"$i")" >> "$tmpfile"
done

while :; do ## loop every $gap seconds
for i in "$watchdir"/*; do ## for each file
if [ -N "$i" ]; then ## check changed
cnt=$(wc -l <"$i") ## get new line count
oldcnt=$(grep "$i" "$tmpfile") ## get old count
oldcnt=${oldcnt##*,}
if [ "$cnt" -ne "$oldcnt" ]; then ## if not equal, print
printf " lines chanaged : %s -> %s (%s)\n" \
"$oldcnt" "$cnt" "$i"
## update tmpfile with new count
sed -i "s|^${i}[,][0-9][0-9]*.*$|${i},$cnt|" "$tmpfile"
fi
fi
done
sleep $gap
done

使用/输出

启动watchdir.sh

$ ./watchdir2.sh dat/tmp

在第二个终端修改文件

$ echo "newline" >> ~/scr/tmp/stack/dat/tmp/testfile.txt

等待 $gap 过期(如果更改两次 - 它不会注册)

$ echo "newline" >> ~/scr/tmp/stack/dat/tmp/testfile.txt

结果

$ ./watchdir2.sh dat/tmp
lines chanaged : 10 -> 11 (dat/tmp/testfile.txt)
lines chanaged : 11 -> 12 (dat/tmp/testfile.txt)

关于linux - 无法回显对文件所做的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36491447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com