Bash 脚本以相反的顺序复制编号的文件
我有一系列文件:
image001.jpg
image002.jpg
image003.jpg
您能帮我编写一个 bash 脚本吗?该脚本以相反的顺序复制图像,以便最终结果是:
image001.jpg
image002.jpg
image003.jpg
image004.jpg <-- copy of image003.jpg
image005.jpg <-- copy of image002.jpg
image006.jpg <-- copy of image001.jpg
括号中的文本不是文件名的一部分。
为什么我需要它?我正在从一系列图像创建视频,并希望视频“向前”播放,然后“向后”播放(循环生成的视频)。
I have a sequence of files:
image001.jpg
image002.jpg
image003.jpg
Can you help me with a bash
script that copies the images in reverse order so that the final result is:
image001.jpg
image002.jpg
image003.jpg
image004.jpg <-- copy of image003.jpg
image005.jpg <-- copy of image002.jpg
image006.jpg <-- copy of image001.jpg
The text in parentheses is not part of the file name.
Why do I need it? I am creating video from a sequence of images and would like the video to play "forwards" and then "backwards" (looping the resulting video).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
printf
打印带有前导 0 的数字。将其放入 for 循环中会产生:
You can use
printf
to print a number with leading 0s.Throwing that into a for loop yields:
我认为我会为此使用一个数组...这样,您就不必硬编码 $MAX 的值。
我使用“bc”进行算术运算,因为 bash 将前导 0 解释为数字是八进制的指示符,并且 bash 中的参数扩展不够强大,无法在不跳圈的情况下剥离它们。我本可以在 sed 中完成此操作,但只要我在 bash 之外调用某些内容,直接进行算术就同样有意义。
我想 Kuegelman 的脚本可以做这样的事情:
不过,该脚本有更大的问题,因为它覆盖了一半的图像:
此外,一旦你超过 007,你就会遇到八进制问题。
I think that I'd use an array for this... that way, you don't have to hard code a value for $MAX.
I used 'bc' for arithmetic because bash interprets leading 0s as an indicator that the number is octal, and the parameter expansion in bash isn't powerful enough to strip them without jumping through hoops. I could have done that in sed, but as long as I was calling something outside of bash, it made just as much sense to do the arithmetic directly.
I suppose that Kuegelman's script could have done something like this:
That script has bigger problems though, because it's overwriting half of the images:
Also, once you get above 007, you run into the octal problem.