非常感谢您的帮助。我编写了一个脚本来重命名所有 .BMP 文件,它似乎有效,但我收到每个文件的以下错误/消息:
IM2019.BMP
mv: cannot stat `IM2000.BMP': No such file or directory
mv: cannot stat `IM2001.BMP': No such file or directory
mv: cannot stat `IM2002.BMP': No such file or directory
mv: cannot stat `IM2003.BMP': No such file or directory
mv: cannot stat `IM2004.BMP': No such file or directory
mv: cannot stat `IM2005.BMP': No such file or directory
mv: cannot stat `IM2006.BMP': No such file or directory
mv: cannot stat `IM2007.BMP': No such file or directory
mv: cannot stat `IM2008.BMP': No such file or directory
mv: cannot stat `IM2009.BMP': No such file or directory
mv: cannot stat `IM2010.BMP': No such file or directory
mv: cannot stat `IM2011.BMP': No such file or directory
mv: cannot stat `IM2012.BMP': No such file or directory
mv: cannot stat `IM2013.BMP': No such file or directory
mv: cannot stat `IM2014.BMP': No such file or directory
mv: cannot stat `IM2015.BMP': No such file or directory
mv: cannot stat `IM2016.BMP': No such file or directory
mv: cannot stat `IM2017.BMP': No such file or directory
mv: cannot stat `IM2018.BMP': No such file or directory
mv: cannot stat `IM2019.BMP': No such file or directory
我在目录中有以下文件:
hristo@HassanGrad27:~/Desktop/Test$ ls
IM0000.BMP IM0011.BMP IM1002.BMP IM1013.BMP IM2004.BMP IM2015.BMP
IM0001.BMP IM0012.BMP IM1003.BMP IM1014.BMP IM2005.BMP IM2016.BMP
IM0002.BMP IM0013.BMP IM1004.BMP IM1015.BMP IM2006.BMP IM2017.BMP
IM0003.BMP IM0014.BMP IM1005.BMP IM1016.BMP IM2007.BMP IM2018.BMP
IM0004.BMP IM0015.BMP IM1006.BMP IM1017.BMP IM2008.BMP IM2019.BMP
IM0005.BMP IM0016.BMP IM1007.BMP IM1018.BMP IM2009.BMP rename
IM0006.BMP IM0017.BMP IM1008.BMP IM1019.BMP IM2010.BMP rename~
IM0007.BMP IM0018.BMP IM1009.BMP IM2000.BMP IM2011.BMP stringtest
IM0008.BMP IM0019.BMP IM1010.BMP IM2001.BMP IM2012.BMP stringtest~
IM0009.BMP IM1000.BMP IM1011.BMP IM2002.BMP IM2013.BMP
IM0010.BMP IM1001.BMP IM1012.BMP IM2003.BMP IM2014.BMP
我可以将我的文件重命名为:
hristo@HassanGrad27:~/Desktop/Test$ ls
cam1-0.BMP cam1-3.BMP cam2-15.BMP cam2-9.BMP cam3-2.BMP
cam1-10.BMP cam1-4.BMP cam2-16.BMP cam3-0.BMP cam3-3.BMP
cam1-11.BMP cam1-5.BMP cam2-17.BMP cam3-10.BMP cam3-4.BMP
cam1-12.BMP cam1-6.BMP cam2-18.BMP cam3-11.BMP cam3-5.BMP
cam1-13.BMP cam1-7.BMP cam2-19.BMP cam3-12.BMP cam3-6.BMP
cam1-14.BMP cam1-8.BMP cam2-1.BMP cam3-13.BMP cam3-7.BMP
cam1-15.BMP cam1-9.BMP cam2-2.BMP cam3-14.BMP cam3-8.BMP
cam1-16.BMP cam2-0.BMP cam2-3.BMP cam3-15.BMP cam3-9.BMP
cam1-17.BMP cam2-10.BMP cam2-4.BMP cam3-16.BMP rename
cam1-18.BMP cam2-11.BMP cam2-5.BMP cam3-17.BMP rename~
cam1-19.BMP cam2-12.BMP cam2-6.BMP cam3-18.BMP stringtest
cam1-1.BMP cam2-13.BMP cam2-7.BMP cam3-19.BMP stringtest~
cam1-2.BMP cam2-14.BMP cam2-8.BMP cam3-1.BMP
这是脚本:
#!/bin/bash
# Specify directory
for i in $(ls *.BMP)
do str="$i"
echo $str
charim=${str:2:1}
#echo $charim
if [ "$charim" -eq 0 ] && [ "$charim" -ne 1 ] && [ "$charim" -ne 2 ]; then
#echo "1"
for j in {0..9}
do mv IM000$j.BMP cam1-$j.BMP
done
for j in {10..19}
do mv IM00$j.BMP cam1-$j.BMP
done
fi
if [ "$charim" -ne 0 ] && [ "$charim" -eq 1 ] && [ "$charim" -ne 2 ]; then
#echo "2"
for j in {0..9}
do mv IM100$j.BMP cam2-$j.BMP
done
for j in {10..19}
do mv IM10$j.BMP cam2-$j.BMP
done
fi
if [ "$charim" -ne 0 ] && [ "$charim" -ne 1 ] && [ "$charim" -eq 2 ]; then
#echo "3"
for j in {0..9}
do mv IM200$j.BMP cam3-$j.BMP
done
for j in {10..19}
do mv IM20$j.BMP cam3-$j.BMP
done
fi
done
什么原因导致错误/警告?
多谢
答案1
for f in IM*.BMP; do
n1=${f:2:1}
n2=${f:3:3}
new="cam$(( n1 + 1 ))-$(( 10#$n2 )).BMP"
echo mv "$f" "$new"
done
$(( ... ))
是算术扩展。10#$n2
强制将 n2 变量的内容作为十进制数处理,并删除前导零:您不能使用,$(( n2 + 0 ))
因为您会得到 n2 == 008 的“无效八进制”错误。$
在算术表达式中是可选的,除非您使用根据# 符号