我写了个批量改图片名的脚本,其中有一句
mv $pic ${a}.jpg
((a+=1))
变量a初值为1,这样改名为1.jpg,2.jpg。但我想改成类似001.jpg这种格式,共三位,不足前面补0.
写成mv $pic `printf XXX a`.jpg的形式行不,格式该咋样啊?
我用手机码的字,说的不是很清楚,凑和着看吧。
批量改名脚本求助。
- josephyoung
- 帖子: 158
- 注册时间: 2011-11-05 18:53
- 来自: 南极圈
Re: 批量改名脚本求助。
代码: 全选
mv $pic `printf "%03d" $a`.jpg
- adam8157
- 帖子: 2794
- 注册时间: 2009-03-05 16:31
- 联系:
- eexpress
- 帖子: 58428
- 注册时间: 2005-08-14 21:55
- 来自: 长沙
-
- 帖子: 1453
- 注册时间: 2008-05-24 8:30
Re: 批量改名脚本求助。
我常用的另一个办法是,让a从1001开始,并把 ${a} 改为 ${a#1} 以便去掉前面的“1”。
-
- 帖子: 398
- 注册时间: 2009-06-28 10:06
- 系统: ubuntu12.04
Re: 批量改名脚本求助。
谢谢各位解答。我再问个问题,如果我在a目录中放有1.sh,2.sh,2.sh中应该可以调用1.sh这个脚本吧,
如 ./1.sh 这样。
现在我手边没有电脑,无法实验,只好提出假设。另外 .和source均可设置工作目录,source可以影响到子shell,是不是这样啊。问题比较多,比较乱。先谢谢各位了。
如 ./1.sh 这样。
现在我手边没有电脑,无法实验,只好提出假设。另外 .和source均可设置工作目录,source可以影响到子shell,是不是这样啊。问题比较多,比较乱。先谢谢各位了。
Still Alive
- Think1st
- 帖子: 45
- 注册时间: 2012-02-07 23:08
Re: 批量改名脚本求助。
请问这个是什么用法?aerofox 写了:我常用的另一个办法是,让a从1001开始,并把 ${a} 改为 ${a#1} 以便去掉前面的“1”。
${a#1}
在这里,问题比答案更抢手。
-
- 帖子: 398
- 注册时间: 2009-06-28 10:06
- 系统: ubuntu12.04
Re: 批量改名脚本求助。
同问。Think1st 写了:请问这个是什么用法?aerofox 写了:我常用的另一个办法是,让a从1001开始,并把 ${a} 改为 ${a#1} 以便去掉前面的“1”。
${a#1}
Still Alive
-
- 帖子: 1453
- 注册时间: 2008-05-24 8:30
Re: 批量改名脚本求助。
Think1st 写了:请问这个是什么用法?aerofox 写了:我常用的另一个办法是,让a从1001开始,并把 ${a} 改为 ${a#1} 以便去掉前面的“1”。
${a#1}
代码: 全选
${parameter#word}
${parameter##word}
Remove matching prefix pattern. The word is expanded to produce a pattern just as in pathname expansion. If the pat‐
tern matches the beginning of the value of parameter, then the result of the expansion is the expanded value of parame‐
ter with the shortest matching pattern (the ``#'' case) or the longest matching pattern (the ``##'' case) deleted. If
parameter is @ or *, the pattern removal operation is applied to each positional parameter in turn, and the expansion is
the resultant list. If parameter is an array variable subscripted with @ or *, the pattern removal operation is applied
to each member of the array in turn, and the expansion is the resultant list.