bash脚本复制以相反的顺序编号的文件 [英] Bash script to copy numbered files in reverse order

查看:144
本文介绍了bash脚本复制以相反的顺序编号的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有文件序列:

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).

推荐答案

我想我会用一个数组这个......这样,你不必为$硬codeA值MAX。

I think that I'd use an array for this... that way, you don't have to hard code a value for $MAX.

image=( image*.jpg )
MAX=${#image[*]}
for i in ${image[*]}
do
   num=${i:5:3} # grab the digits
   compliment=$(printf '%03d' $(echo $MAX-$num | bc))
   ln $i copy_of_image$compliment.jpg
done

我使用BC算术因为bash的跨$ P $点前导零作为一个指标,该数字是八进制和参数扩展在bash是没有强大到足以剥夺他们不通过箍跳。我可以做,在sed的,但只要我在庆典之外调用的东西,这让同样多的意义,直接做算术。

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.

我想这Kuegelman的脚本可以做这样的事情:

I suppose that Kuegelman's script could have done something like this:

MAX=(ls image*.jpg | wc -l)

这脚本有更大的问题,但因为它的图像覆盖一半:

That script has bigger problems though, because it's overwriting half of the images:

cp image001.jpg image006.jpg # wait wait!!! what happened to image006.jpg???

此外,一旦你得到007以上,你碰上了八进制的问题。

Also, once you get above 007, you run into the octal problem.

这篇关于bash脚本复制以相反的顺序编号的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆