Re: Uppercasing filenames
On Saturday 20 September 2003 23:06, Jeff Elkins wrote:
> I need to convert a bunch of filenames to uppercase. I tried the script
> below, which does...nothing.
>
> #!/bin/sh
> for file in $*
> do
> if [ -f $file ]
> then
> ucfile=`echo $file | tr [a-z] [A-Z]`
> if [ $file != $ucfile ]
> then
> mv -i $file $ucfile
> fi
> fi
> done
Let's ignore the scripts intended purpose for a while and talk about what will
happen when it's trying to process a filename with a space character...
Quote all uses of variables:
if [ -f "$file" ]
Otherwise, if you have a file called 'long name', you would get the translated
name 'LONG NAME' but the mv command would be expanded to
mv long name LONG NAME
This is NOT what you want.
This seems to work (use mv instead of echoing the results):
#!/bin/sh
for i in $*; do
if [ -f "$i" ]; then
ucfile=`echo "$i"|tr [a-z] [A-Z]`
if [ "$i" != "$ucfile" ]; then
echo "$ucfile"
fi
fi
done
--
Got Backup?
Reply to: