分页: 1 / 1
批量改名脚本求助。
发表于 : 2012-03-02 10:04
由 zcf115
我写了个批量改图片名的脚本,其中有一句
mv $pic ${a}.jpg
((a+=1))
变量a初值为1,这样改名为1.jpg,2.jpg。但我想改成类似001.jpg这种格式,共三位,不足前面补0.
写成mv $pic `printf XXX a`.jpg的形式行不,格式该咋样啊?
我用手机码的字,说的不是很清楚,凑和着看吧。
Re: 批量改名脚本求助。
发表于 : 2012-03-02 10:16
由 josephyoung
代码: 全选
mv $pic `printf "%03d" $a`.jpg
Re: 批量改名脚本求助。
发表于 : 2012-03-02 10:37
由 adam8157
Re: 批量改名脚本求助。
发表于 : 2012-03-02 12:38
由 eexpress
gprename
Re: 批量改名脚本求助。
发表于 : 2012-03-02 13:00
由 aerofox
我常用的另一个办法是,让a从1001开始,并把 ${a} 改为 ${a#1} 以便去掉前面的“1”。
Re: 批量改名脚本求助。
发表于 : 2012-03-02 13:27
由 zcf115
谢谢各位解答。我再问个问题,如果我在a目录中放有1.sh,2.sh,2.sh中应该可以调用1.sh这个脚本吧,
如 ./1.sh 这样。
现在我手边没有电脑,无法实验,只好提出假设。另外 .和source均可设置工作目录,source可以影响到子shell,是不是这样啊。问题比较多,比较乱。先谢谢各位了。
Re: 批量改名脚本求助。
发表于 : 2012-03-02 15:11
由 Think1st
aerofox 写了:我常用的另一个办法是,让a从1001开始,并把 ${a} 改为 ${a#1} 以便去掉前面的“1”。
请问这个是什么用法?
${a#1}
Re: 批量改名脚本求助。
发表于 : 2012-03-02 15:43
由 zcf115
Think1st 写了:aerofox 写了:我常用的另一个办法是,让a从1001开始,并把 ${a} 改为 ${a#1} 以便去掉前面的“1”。
请问这个是什么用法?
${a#1}
同问。
Re: 批量改名脚本求助。
发表于 : 2012-03-02 19:34
由 aerofox
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。