小白我初學shell編程,自己練習寫了如下的批量改名的腳本,但是對文件名中存在空格的文件無效,該如何解決呢?代碼如下(關鍵代碼為紅色):
#!/bin/sh
##################################################
#rename one or more file names
##################################################
#display help information
displayHelp(){
echo "FORMAT:"
echo " rename [directory] exetend name"
echo "FUNCTION:"
echo " rename many files one time"
echo "PERAMETERS:"
echo " [directory] : the directory of the files you want to rename "
echo " extend : the extend name of the files you want to rename"
echo " name : new file name of the files you want to rename"
exit 0;
}
#perameter missing or wrong
usage(){
echo "usage:rename [directory] exetend newname"
echo "for more information:rename --help"
exit 0
}
case "$#" in
#if number of perameters is 1
1)
if [ $1="--help" ]
then
displayHelp
else
usage
fi
;;
#if number of perameters is 2
2)
directory='./'
extend=$1
newname=$2
;;
#if number of perameters is 3
3)
directory=$1
extend=$2
newname=$3
;;
*)
usage
;;
esac
count=1
for file in `ls *.$extend`
do
mv "$file" "$newname$count.$extend"
count=$(($count+1))
done
exit 0
#script ends