This script takes the first parameter (which is to be the new extension) and each parameter after that is a file name (which will have its old extension turned into the new one. Mind you, this script works almost perfectly.
However, the test script (for me to turn the assignment in) forces this line to call the script running from the "filderFilesAreIn" directory:
./theScriptName.sh 'dat' 'file1.txt' 'file2.cpp' 'file3.dat' '../folderFilesAreIn/file4.dat'
My script does the first 3 files flawlessly. The 4th file, though, my code produces this output:
mv: `../folderFilesAreIn/file4.dat' and `../folderFilesAreIn/file4.dat' are the same file
In my code, I've used "mv -f" which is supposed to force the command to overwrite the file. It does this with the first 3 files, but the fourth (I'm guessing due to the directory path in front of it) displays this error. This error isn't causing the program to not work but the test script (the one that grades the assignment for pass/fail) isn't letting me pass as this output is "unexpected" by it.
Any help would be appreciated! I can't seem to find a way to cause "mv" to not produce any output. If I could silence the output or use another command to replace the file without getting any extra output it should count.
Thanks for your help! It's very frustrating and the teacher is being no help.
#!/bin/sh
newExt=$1
shift
for x in $*
do
oldName=$x
olderName=$( echo "$oldName" | sed "s/ /_/g" )
dir=$(dirname $oldName)
currentdir=$(pwd)
if [ $dir = '.' ]
then
dir="$currentdir"
fi
if [ -f "$oldName" ];
then
preExt=$(echo $olderName | sed 's/^.*\.//')
fileName=$(basename $olderName .${preExt})
newName=${fileName}.${newExt}
fixName=$( echo "$olderName" | sed "s/_/ /g" )
fixNewName=$( echo "$newName" | sed "s/_/ /g" )
if [ "$fixName" = "$fixNewName" ];
then
oldName=$oldName
else
mv -f "$fixName" $dir/"$fixNewName"
fi
else
echo "$oldName: No such file"
fi
done

New Topic/Question
Reply




MultiQuote





|