Art Alexion on 22 Feb 2006 16:42:49 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] bash script help needed


Dan Crosta wrote:

> Art Alexion wrote:
>
>> I am trying to iron out a script that renames all files in a current
>> directory to replace embedded spaces with underscores, and makes
>> everything lower case.
>>
>> The suggestion that I was given was (in pertinent part):
>>
>> for OLD in *
>>   do
>>     NEW=`echo $OLD|sed s/\ /\_/g | tr [:upper:] [:lower:]`
>>     echo "OLD=\"$OLD\""
>>     echo "NEW=\"$NEW\""
>>     echo -e "mv \"$OLD\" \"$NEW\"\n"
>>   done
>>
>> But while the messages sent to the console indicated what I wanted to do
>> was happening, and there were no errors reported, no changes were
>> actually being made to the filenames.
>>
>> I read the echo man page and it seemed that the line
>>
>> echo -e "mv \"$OLD\" \"$NEW\"\n"
>>
>> didn't actually execute the mv command, so I added the line
>>
>> mv $OLD $NEW
>>
>> after it, so it now reads
>>
>> for OLD in *
>>   do
>>     NEW=`echo $OLD|sed s/\ /\_/g | tr [:upper:] [:lower:]`
>>     echo "OLD=\"$OLD\""
>>     echo "NEW=\"$NEW\""
>>     echo -e "mv \"$OLD\" \"$NEW\"\n"
>>     mv $OLD $NEW
>>   done
>>
>> But this results in the error
>>
>> mv: when moving multiple files, last argument must be a directory
>>
>> even though $NEW does not yet exist, so I am not really moving multiple
>> files.
>>
>> What am I doing wrong?
>
>
> I think what it might need is
>
> mv "$OLD" "$NEW".

That solved the problem.  As Sean Finney said:

>	OLD="unclean name"
>	NEW="unclean_name"
>	mv $OLD $NEW
>
>will result in
>
>	mv unclean name unclean_name
>
Which is kinda odd because then the problem wasn't so much that
unclean_name wasn't a directory as the error suggested, but that unclean
and name didn't exist as separate files and I would have expected a
"file not found" error instead.

Anyway, making the change solved the problem in practice, but due to the
foregoing, I am still confused as to what was happening.  I am going to
assume that mv, in its error handling first detected that there were
multiple "file" arguments and that unclean_name did not exist as a
subdirectory and returned that error before it tested to see if unclean,
name and unclean_name existed as files.

-- 

_______________________________________
Art Alexion
Arthur S. Alexion LLC

PGP fingerprint: 52A4 B10C AA73 096F A661  92D2 3B65 8EAC ACC5 BA7A
The attachment -- signature.asc -- is my electronic signature; no need for alarm.
Info @ http://mysite.verizon.net/art.alexion/encryption/signature.asc.what.html

Key for signed PDFs available at
http://mysite.verizon.net/art.alexion/encryption/ArthurSAlexion.p7c
The validation string is TTJY-ZILJ-BJJG.
________________________________________

Attachment: signature.asc
Description: OpenPGP digital signature

___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug