批量改名脚本求助。

sh/bash/dash/ksh/zsh等Shell脚本
回复
zcf115
帖子: 398
注册时间: 2009-06-28 10:06
系统: ubuntu12.04

批量改名脚本求助。

#1

帖子 zcf115 » 2012-03-02 10:04

我写了个批量改图片名的脚本,其中有一句
mv $pic ${a}.jpg
((a+=1))
变量a初值为1,这样改名为1.jpg,2.jpg。但我想改成类似001.jpg这种格式,共三位,不足前面补0.
写成mv $pic `printf XXX a`.jpg的形式行不,格式该咋样啊?
我用手机码的字,说的不是很清楚,凑和着看吧。
Still Alive
头像
josephyoung
帖子: 158
注册时间: 2011-11-05 18:53
来自: 南极圈

Re: 批量改名脚本求助。

#2

帖子 josephyoung » 2012-03-02 10:16

代码: 全选

 mv $pic `printf "%03d" $a`.jpg
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: 批量改名脚本求助。

#4

帖子 eexpress » 2012-03-02 12:38

gprename
● 鸣学
aerofox
帖子: 1453
注册时间: 2008-05-24 8:30

Re: 批量改名脚本求助。

#5

帖子 aerofox » 2012-03-02 13:00

我常用的另一个办法是,让a从1001开始,并把 ${a} 改为 ${a#1} 以便去掉前面的“1”。
zcf115
帖子: 398
注册时间: 2009-06-28 10:06
系统: ubuntu12.04

Re: 批量改名脚本求助。

#6

帖子 zcf115 » 2012-03-02 13:27

谢谢各位解答。我再问个问题,如果我在a目录中放有1.sh,2.sh,2.sh中应该可以调用1.sh这个脚本吧,
如 ./1.sh 这样。
现在我手边没有电脑,无法实验,只好提出假设。另外 .和source均可设置工作目录,source可以影响到子shell,是不是这样啊。问题比较多,比较乱。先谢谢各位了。
Still Alive
头像
Think1st
帖子: 45
注册时间: 2012-02-07 23:08

Re: 批量改名脚本求助。

#7

帖子 Think1st » 2012-03-02 15:11

aerofox 写了:我常用的另一个办法是,让a从1001开始,并把 ${a} 改为 ${a#1} 以便去掉前面的“1”。
请问这个是什么用法?
${a#1}
在这里,问题比答案更抢手。
zcf115
帖子: 398
注册时间: 2009-06-28 10:06
系统: ubuntu12.04

Re: 批量改名脚本求助。

#8

帖子 zcf115 » 2012-03-02 15:43

Think1st 写了:
aerofox 写了:我常用的另一个办法是,让a从1001开始,并把 ${a} 改为 ${a#1} 以便去掉前面的“1”。
请问这个是什么用法?
${a#1}
同问。
Still Alive
aerofox
帖子: 1453
注册时间: 2008-05-24 8:30

Re: 批量改名脚本求助。

#9

帖子 aerofox » 2012-03-02 19:34

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.
以上摘自bash的手册。具体到我那例子,就是 $a 的结果中去掉开头的 1。
回复