OSX Bash Script working with Files/Folders with spaces in the name

I'm trying to build a BASH script on OS X (10.6/10.7) to process a folder called QCExports which has folders with people's names in it in the format "LAST, First", i.e. "BOND, James".

When I run the following script, everything works but it falls over on folder or filenames with a space in them.

Script Code:

#!/bin/bash
echo "QuickCeph Export Script"

#Set Path to Process & Paths to Copy To
pathToQCExports=/Users/myuser/Desktop/QCExports
sureSmilePath=/Users/myuser/Desktop/QCExportsForSureSmile
sesamePath=/Users/myuser/Desktop/QCExportsForSesame
blankReplace=""

#Process Each Name
find $pathToQCExports -type d | while read name ; do

    #escaping the folder with a space in the name
nameParsed=${name/", "/",\ "}

    echo "Processing: "$nameParsed  
    pathSureSmile=${nameParsed/$pathToQCExports/$sureSmilePath}
    pathSesame=${nameParsed/$pathToQCExports/$sesamePath}
    mkdir $pathSesame
    mkdir $pathSureSmile

    echo "Folders Created"

    #Copy For SureSmile 
    echo ${pathSureSmile}"/Suresmile-Frontal\ Photo.jpg" ${pathSureSmile}"/Suresmile-Frontal\ Photo.jpg"
    #cp `${$pathSureSmile}"/Suresmile-Frontal\ Photo.jpg" ${pathSureSmile}"/Suresmile-Frontal\ Photo.jpg"`

    #Copy For Sesame
    echo ${pathSesame}"/Suresmile-Frontal\ Photo.jpg" ${pathSesame}"/S02.jpg"
    #cp `${pathSesame}"/Suresmile-Frontal\ Photo.jpg" ${pathSesame}"/S02.jpg"`

done

echo "Completed";

Output:

QuickCeph Export Script
Processing: /Users/myuser/Desktop/QCExports/BOND,\ James
mkdir /Users/myuser/Desktop/QCExportsForSesame/BOND,\ James
mkdir: James: File exists
mkdir /Users/myuser/Desktop/QCExportsForSureSmile/BOND,\ James
mkdir: James: File exists
Folders Created
/Users/myuser/Desktop/QCExportsForSureSmile/BOND,\ James/Suresmile-Frontal\ Photo.jpg /Users/myuser/Desktop/QCExportsForSureSmile/BOND,\ James/Suresmile-Frontal\ Photo.jpg
/Users/myuser/Desktop/QCExportsForSesame/BOND,\ James/Suresmile-Frontal\ Photo.jpg /Users/myuser/Desktop/QCExportsForSesame/BOND,\ James/S02.jpg
Completed

On OS X usually in the terminal, you use a \ to escape a space in a folder or filename, but that doesn't seem to work.

I notice that it's interpreting the spaces as a normal space would be interpreted on the command line and thinking I want to execute the command on two files - i.e. it's not passing the \ onwards. I end up with a folder called "Bond,\" and a folder called "James" in the folder I execute the script from.

Note, I deliberately have the cp commands # out at the moment, so they aren't being executed... the problem is the same for both creating the folders & copying the filenames.

If I copy and paste the "echo'd" version of these commands into a terminal window, the commands work! But when BASH executes them, it doesn't respect the . :(

Any ideas?

Thanks!! John

Answers


See my modifications on your script, you don't have to substitute spaces like you tried. Moreover, you must choose if you backslash the spaces or if you are using quotes. The simple way is to use double quotes. Good doc about quotes, see http://mywiki.wooledge.org/Quotes and http://wiki.bash-hackers.org/syntax/words

#!/bin/bash
echo "QuickCeph Export Script"

#Set Path to Process & Paths to Copy To
pathToQCExports=/Users/myuser/Desktop/QCExports
sureSmilePath=/Users/myuser/Desktop/QCExportsForSureSmile
sesamePath=/Users/myuser/Desktop/QCExportsForSesame
blankReplace=""

#Process Each Name
find $pathToQCExports -type d | while read nameParsed ; do

    echo "Processing: $nameParsed"
    pathSureSmile="${nameParsed/$pathToQCExports/$sureSmilePath}"
    pathSesame="${nameParsed/$pathToQCExports/$sesamePath}"
    mkdir "$pathSesame"
    mkdir "$pathSureSmile"

    echo "Folders Created"

    #Copy For SureSmile 
    echo "${pathSureSmile}/Suresmile-Frontal Photo.jpg" "${pathSureSmile}/Suresmile-Frontal Photo.jpg"

    #Copy For Sesame
    echo "${pathSesame}/Suresmile-Frontal Photo.jpg" "${pathSesame}/S02.jpg"

done

echo "Completed"

Need Your Help

How to make progress bar and load database running together in splash screen?

android database sqlite progress-bar splash-screen

I have splash screen which can load database sqlite before go to main activity. And my problem, progress bar layout run after loaded database. If we lauch app, app become blank layout (load databas...

I want to implement fire-and-forget request for in img src in JavaScript

javascript image src onerror

I'm trying to implement fire-and-forget on an img src=... call from a web page, and am looking for the most solid method. Why?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.