我已经设置了 incron 来监视目录,如果在此目录中创建文件,它会触发 bash 脚本:
/home/benji/Nextcloud/Buchhaltung/test/ IN_CREATE ./media/benji/Storage/Programme/Scripts/autoconvert/nc_autoconvert_function
不幸的是,每次脚本启动并执行其工作时,我都会将多个文件复制到此目录中。到目前为止一切顺利,因为这似乎是 Incron 的用途。就我而言,问题是,脚本包含一些循环,这些循环位于 incron 监视的目录中的文件上。一些“太慢”而无法处理的文件在第二次或任何后续执行相同脚本时被重新处理,因此文件被复制。我想知道有什么好的解决方案可以解决这个问题?
脚本本身如下:
#!/bin/bash
#Variables for you to declare!
dir="/home/benji/Nextcloud/Buchhaltung/test/"
tag="Buchung"
images="(*.png|*.jpg|*.jpeg|*.JPG|*.JPEG|*.webp)"
format=".pdf"
dateformat=+"%Y"-"%m"-"%d"
convert="convert"
copy="cp"
#Preperations
shopt -s extglob
#sleep 10
#Actual script starting here – do not change unless you know what you are doing ;)
function process_files() {
command=$1
filestoproccess=$2
for f in $dir*$filestoproccess; do #loop on every file that has extension $images
if [[ "$f" != *$tag* ]]; then #exclude files of the loop with filename including $tag
mdate="$(date -r "$f" $dateformat)"
if [[ -f $dir""$tag"_"$mdate$format"" ]]; then #checks if "Buchung_$mdate.pdf" matches an already existing file in $dir
for e in $f; do
collisions="$(ls "$dir" | grep "$mdate")"
max=$(echo ${collisions//@($tag|$mdate|$format|_)} | tr " " "\n" | sort -nr | head -1)
suffix=$(expr $max + 1)
$command "$e" "$dir""$tag"_"$mdate"_"$suffix$format"
rm "$e"
done
else
$command "$f" "$dir""$tag"_"$mdate""$format"
rm "$f"
fi
else
echo $f" - Already processed!"
fi
done
}
process_files $convert $images
process_files $copy $format