
April 18th, 2008, 04:37 AM
|
|
Registered User
|
|
Join Date: Apr 2008
Posts: 1
Time spent in forums: 9 m 56 sec
Reputation Power: 0
|
|
|
Dos style rename
Hey guys i'm creating a dos style rename script, so if a user types say q14.* as the 1st param and b14.* as the 2nd and will rename all q14 files to b14 but keep the extensions, so i've developed nearly the full script "i think", if i use echo(echo "if $1 had been renamed it would now be $newfile") to simulate the rename it works fine shows
Quote: q14.1 will be renamed to b14.1
q14.2 will be renamed to b14.2
.....etc |
but it becomes a whole different story if i alter that line to "mv $1 $newFile"
Code:
#!/bin/sh
case $# in
0)echo Usage: dosRename file >&2
;;
*)for file
do
case $file in
[A-Za-z0-9]*\.\*) type=main
;;
\*\.[A-Za-z0-9]* )type=extension
;;
esac
#echo $type
done
endFile=$file
while [ $# -ne 1 ]
do
case $type in
'extension') extension=`echo $endFile |sed -e 's/\*\./\./'`
newfile=`echo $1 |sed -e "s/\.[A-Za-z0-9]*/$extension/"`
echo "if $1 had been renamed it would now be $newfile"
# mv $1 $newfile
;;
'main') fileName=`echo $endFile | sed -e 's/\.\*//'`
newFile=`echo $1 | sed -e "s/[A-Za-z0-9]*\.\(.*[A-Za-z0-9]*\)/$fileName\.\1/"`
echo "if $1 had been renamed it would now be $newFile"
# mv $1 $newFile
;;
esac
shift 1
done
;;
esac
it renames the 1st one fine then the rest go along the lines of
dosRename q14.* b14.*
it dose first one fine but 2nd one ends up q14.1.2 and third q14.1.2.3
any ideas what is up with my script?
|