whats the correct way to loop this

99封情书 提交于 2019-12-12 06:58:00

问题


I have a script where inotifywait is piped into a while loop that executes the following logic.

     cp "$S3"/2/post2.png "$S3";
     mv "$S3"/1/post1.png "$S3"/2/post2.png;
     cp "$S3"/3/post3.png "$S3";
     mv "S3"/post2.png "$S3"/3/post3.png;

so forth and so on..... then at the end of the script...

     mv "$dir"/$file "$S3"/1/post1.png

That line represents a fresh post, the above is the rotation of older post.

I can can hand code the iterations all the way down to 100+, but I would like to program more efficiently and save time.

So, what's some correct ways to loop this?


回答1:


I think a better mechanism would list the directories in "$S3" in reverse numeric order, and arrange to process them like that. It isn't clear if the 100 directories are all present or whether they need to be created. We'll assume that directories 1..100 might exist, and directory N will always and only contain postN.png.

I'm assuming that there are no spaces, newlines or other awkward characters in the file paths; this means that ls can be used without too much risk.

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

The cd "$S3" means I don't get a possibly long pathname included in the output; the ls */*.png lists the files that exist; the sed removes the file name and slash, leaving just a list of directory numbers containing files; and the sort puts the directories in reverse numeric order.

The rest is straight-forward, given the assumption that the necessary directories already exist. It would not be hard to add [ -d "$S3/$next" ] || mkdir -p "$S3/$next" before moving the file. Clearly, after the loop you can use your final command:

mv "$dir/$file" "$S3/1/post1.png"

Note that I've enclosed complete names in double quotes; it generally leads to fewer nasty surprises if something acquires spaces unexpectedly.




回答2:


Try this:

for i in $(ls -r1 "$3"); do 
    mkdir -p "$3/$((i+1))"
    mv "$3/$i/post$i.png" "$3/$((i+1))/post$((i+1)).png"
done
mv "$dir"/$file "$S3"/1/post1.png

The loop will iterate through all directories in reverse order and move the files.



来源:https://stackoverflow.com/questions/13097606/whats-the-correct-way-to-loop-this

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!