
次のスクリプトがあります。
#!/bin/bash
y=$(ls -t ./pics/0/*.png | head -n 2 | tail -n 1)
new=$(ls -t ./pics/0/*.png | head -n 1)
while true
do
if cmp --silent "$y" "$new" ; then
y=$(ls -t ./pics/0/*.png | head -n 1)
base64 $y | tr -d '\n' | sed '$ a \'
new=$(ls -t ./pics/0/*.png | head -n 1)
fi
done
私は何が間違っていましたか?明確に言うと、私の目標は、最新のファイルが以前の最新のファイルと異なるか、そして一意のBASE64をSTDOUTとして生成する場合にのみ比較することです(つまり、一度だけ印刷する必要があることを意味します)。
答え1
次のコードスニペットは、スペース、タブ、または改行を含むファイル名には機能しません。
find
ここで使用される - コマンドの詳細については、次を参照してください。https://unix.stackexchange.com/a/240424/364705
#!/bin/bash
lastrun=''
while true; do
read -r newer older <<< \
$(find . -type f -exec stat -c '%Y %n' {} \; \
| sort -nr \
| awk 'NR==1,NR==2 {print $2}'\
| xargs )
if [ "$newer" == "$lastrun" ]; then
:
else
if ! cmp "$newer" "$older" > /dev/null ; then
base64 "$newer"| tr -d '\n' | sed '$ a \'
lastrun="$newer"
fi
fi
done
そして使用された解決策は次のとおりですinotify-wait
。
#!/bin/bash
lastfile=''
inotifywait -m /path/to/somewhere -e close -e moved_to |
while read path action file; do
if [ "$lastfile" != '' ];then
if ! cmp "${path}$file" "${path}${lastfile}" > /dev/null ; then
base64 "${path}${file}"
fi
fi
lastfile="$file"
done