I've reduced my block of code to the following example. It works when the variable $backupPath
does not contain a space character, but fails when it does. I've spent a significant amount of time studying the resources below and have tried many variations of the examples there.
I believe that I may need some combination of escape characters and command expansion, perhaps using the $( )
syntax, but I cannot seem to find the right solution.
How can I modify this code such that it will work for all possible characters in $backupPath
?
backupFolderPath=$HOME"/Desktop/folder"
domain=".domain.com";
username="user";
destination[10]="subdomain";
backupPath[10]="/Volumes/backup drive";
i=10
findCommand[$i]='find '${backupPath[$i]}' -name "*partfilename*" -type f -print0 | xargs -0 stat -f "%m %N" | sort -rn | head -1 | cut -f2- -d" "'
echo ${findCommand[$i]} #looks OK
filePath[$i]=`ssh $username@${destination[$i]}$domain "${findCommand[$i]}"`
echo ${filePath[$i]} # empty when $backupPath contains a space
rsync -avz $username@${destination[$i]}$domain:"${filePath[$i]}" $backupFolderPath # fails when $filePath is empty
Resources:
Expansion of variable inside single quotes in a command in bash shell script
BASH Script to cd to directory with spaces in pathname
How to input a path with a white space?
How to set a variable to the output from a command in Bash?
http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_04.html