我有下面这个脚本将被守护进程并触发可能数百,如果不是不同的用户数千次。bash脚本优化
该脚本使用inotifywait
观看上传文件夹,然后将上传的文件移动到其最终目的地以进行演示,在旋转(备份/移动)之前的上传之后。该代码将针对不同的上传文件夹运行。
#!/bin/bash
db="$VAR1"/path/to/upload/folder/
s3="$VAR2"/path/to/remote/folder
inotifywait -m -r -e attrib "$db" |
while read dir ev file;
do
for dirnum in $(cd "$s3"; ls */*.png | sed 's%/.*%%' | sort -nr)
do
next=$(($dirnum + 1));
mv "$s3/$dirnum/post$dirnum.png" "$s3/$next/post$next.png";
done
mv "$db"/"$file" "$s3"/1/post1.png
done
我能做些什么来优化它?还是应该重写一个更快的编程语言?另外,如何在一定的负载下测试脚本?
如果速度够快,则不需要重写。你可以通过向其中扔文件来测试它。一个明显的优化是跟踪每个目录中的计数(WTF是“文件夹”?),而不是每次计算它。 –
相关,但扩展名为[什么是正确的方式来循环此?](http://stackoverflow.com/questions/13097606/whats-the-correct-way-to-loop-this/) –