简体   繁体   中英

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", ie "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 - ie 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

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM