[TriLUG] bash spaces in filenames

Chander Ganesan chander at otg-nc.com
Tue May 23 18:00:06 EDT 2006


James Tuttle wrote:
> I'm need to grab a few hundred songs out of a few thousand to dump to my
> wife's IPod.  I have a list of the files she selected and I wrote a
> little bash script to grab them and sync them to her IPod, but it fails
> on spaces in filenames.  Probably on things like ' and & and - also.
> Any ideas?
>
>
> #/bin/env bash
>
> FILES="Barry White - Let's Get It On.mp3
> Barry White - Shaft Theme.mp3"
>
> for file in $FILES
>   do
>     if [ -e $file ]; then
>       FILE_LIST="$file $FILE_LIST"
>     else
>       echo "$file doesn't exist"
>     fi
>   done
>
> snip
>
>   
The space thing is easy...just use a different delimiter (like a colon 
":").  For example:

IFS=":"
FILES="Barry White - Let's Get It On.mp3:Barry White - Shaft Theme.mp3"

for file in $FILES
  do
    if [ -e $file ]; then
      FILE_LIST="$file $FILE_LIST"
    else
      echo "$file doesn't exist"
    fi
  done

If you have them all in a file you can do something like this:

IFS="$"
for file in $(cat -vE mp3files); do
   if [ -e $file ]; then
     FILE_LIST="$file:$FILE_LIST"
   else
...

and then change the IFS character to a ":" for the rest...


Chander Ganesan
Open Technology Group, Inc.
One Copley Parkway, Suite 210
Morrisville, NC  27560
Phone: 877-258-8987/919-463-0999




> Thanks,
> Jim
>   



More information about the TriLUG mailing list