Hi I came across this post and it has helped me greatly. I am still having trouble with the final shell script though.
"
Finally, within the same directory, create a shell script, chExt.sh that takes one or more parameters, where the first is a desired extension and the remainder are names of files to be renamed. For each file in the command line, this script should rename the file, as above, if the file exists or print the error message described in the previous step if the file does not exist.
For example,
ls > crocodile.foo
echo bark > dingo.bar
./chExt.sh dat crocodile.foo bogusName.foo dingo.bar
should result in crocodile.foo being renamed crocodile.dat, an error message "bogusName.foo: No such file", and dingo.bar being renamed dingo.dat.
"
This is the script so far:
Code:
#!/bin/sh
extension="$1"
shift
for fname in "${@}" ; do
mv "$fname" "${fname%.*}.$extension"
if test -r "$fname" ; then
newname="${fname%.*}.$extension"
if test "$fname" = "$newname" ; then ##This is the test to prevent mv from sit\
ing an error
:
else
mv -f "$fname" "${fname%.*}.$extension"
fi
else
echo "$fname": No such file
fi
done
This gives me the following errors:
./chExt.sh 'cpp' 'aardvark.CPP' 'bongo.dat'
mv: cannot stat `aardvark.CPP': No such file or directory
aardvark.CPP: No such file
mv: cannot stat `bongo.dat': No such file or directory
bongo.dat: No such file
Thanks for all help!