Re: For Loops and Space in Names

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

 



On Tue, 2008-12-09 at 18:53 -0600, Dave Ihnat wrote:
> On Wed, Dec 10, 2008 at 11:17:05AM +1100, Cameron Simpson wrote:
> > is totally reliable and does not need $IFS hacking (which amounts to
> > "guess a char I might not see in a filename).
> 
> Hmm...I don't have as much problem with it, but that may be because
> since I started using Unix around 1980, using IFS to parse records has
> been a useful tool.  Something like:
> 
>   SAVIFS=$IFS
>   IFS=:
>   
>   while read INLINE </etc/passwd
>   do
>   	set $INLINE
> 	(process fields)
>   done;
>   IFS=$SAVIFS
> 
> (Yeah, if it gets too complicated, shift to awk.)

Spaces have always been one of my pet peeves and I find this discussion
rather interesting.  Spaces don't belong in filenames and they make
script writing a pain.  I'm going to include a script I wrote a long
time ago to handle the problem of junk characters in music files.  Even
buying music online from Amazon will get you spaces in filenames :(

I remember messing with this little script quite a bit before it worked.
One lesson I learned writing this was to use lot's of double quotes.
The script replaces spaces (and other things) in filenames.  I just put
it up here as an example since it's pretty short.  I seem to use it a
lot.  

The main for loop is:

    for i in *; do 
        fixname "$i"
    done

Notice the quotes around "$i".  They're important.

Robert


#!/bin/bash
#
# fixmp3names
# Copyright (c) 2000, by Robert Wuest
# Permission is granted to use, modify and, distribute according
# to the terms of the GPL
#
# This script fixes a lot of the anomalies in file names
# usually weird stuff from mp3 files, but it's really generic 
#
# if called with no args it processes all files in the current directory
# or works on the names passed on the command line
# probably doesn't work across directories
#
# just writes the mv commands to standard out
# if you want it to actually do something, pipe the output to sh, as in:
#   fixmp3names | sh
#

IFS=$'\n'

function fixname() 
{
    newname=`echo $1 | \
        sed -r \
            -e "s/[ ]+/_/g" \
            -e "s/[_]+/_/g" \
            -e "s/'//g" \
            -e "s/[+]//g" \
            -e "s/,//g" \
            -e "s/_-_/-/g" \
            -e "s/\&/and/g"`;
    # only do rename if the name is changed
    if [ \"$1\" != \"${newname}\" ]; then
        echo mv \"$1\" \"${newname}\"
    else
        echo "# \"$1\" and \"${newname}\" are the same file"
    fi
}

if [ "$#" == "0" ]; then
    for i in *; do 
        fixname "$i"
    done
else
    while [ "z$1" != "z" ]; do
        fixname "$1"
        shift
    done
fi

exit




-- 
fedora-list mailing list
fedora-list@xxxxxxxxxx
To unsubscribe: https://www.redhat.com/mailman/listinfo/fedora-list
Guidelines: http://fedoraproject.org/wiki/Communicate/MailingListGuidelines

[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux