-1

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

Community
  • 1
  • 1
Bobby
  • 1,585
  • 3
  • 19
  • 42
  • Potentially using `'findCommand[$i]='find "${backupPath[$i]}" ...` could solve your problem? Note that you have not given us something that we can try.. – Tom de Geus Apr 28 '17 at 07:11
  • It wasn't sufficient to replace the single quotes around `${backupPath[$i]}` with double quotes on the line you suggested. I suppose that it's not possible to create a reproducible example with `ssh`. Is there a way, or is there another command I could try that would fill the same purpose here? – Bobby Apr 28 '17 at 07:23
  • By the way, is the title of the post OK? – Bobby Apr 28 '17 at 07:31
  • You should run that code though [shellcheck.net](http://www.shellcheck.net/)... – l'L'l Apr 28 '17 at 17:58
  • You need to work on your understanding of how quotes work. In particular, `backupFolderPath=$HOME"/Desktop/folder"` should be written either `backupFolderPath="$HOME/Desktop/folder"` or `backupFolderPath="$HOME"/Desktop/folder` and you should understand why. – William Pursell May 01 '17 at 23:17

1 Answers1

0

Simplest solution, remove/replace the 'space' from the path. As you have 'discovered', special chars in file names/paths require special attention and can be quite time consuming to resolve...

Note that you should use double quotes or your variables may not 'expand':

"${backupPath[$i]}" ## this should work vs 
'${backupPath[$i]}' ## this should NOT work

The OS + Shell combination can make a difference. Possible solutions might be:

  • escape the space i.e. "/Volumes/backup\ drive"
  • modify the value of IFS (Internal Field Separator); default value is: [space][tab][newline] == any of these characters will cause a similar issue... It you take this approach then remember to save, modify + use, and then restore the original value since you could 'break' something else the comes AFTER you tinker with IFS...

:)
Dale

Dale_Reagan
  • 1,953
  • 14
  • 11