0

I'm working on my dotfiles scripts, and am creating a restore.sh script to copy preferences, folders, etc. after a clean install.

I've created a check to see if a drive name was entered as an argument, and if not to use a default drive. But!... the default drive has spaces in the filename.

So I've created a test script to copy a file from /Users/tim/My Folder/file.txt to ~/Desktop/file.txt

if [[ $# -eq 0 ]];
    then
    dir="My\ Folder"
else
    dir=$1
fi

options=(-a -v -h -W --progress --compress-level=0)

rsync "${options[@]}" "/Users/tim/$dir/file.txt" "~/Desktop/file.txt"

That doesn't work, so I've been playing with variations on this

from="/Users/tim/$dir/file.txt"
to="~/Desktop/file.txt"
options=(-a -v -h -W --progress --compress-level=0 $from $to)
rsync "${options[@]}"

But I continually get the following error

rsync: link_stat "/Users/tim/My\ Folder/file.txt" failed: No such file or directory (2)

I've been reading up on this and thought arrays were the way to add spaces in variables, but so far nothing I'm trying is working.

timgavin
  • 4,972
  • 4
  • 36
  • 48
  • Have you tried removing the backslash in `dir="My\ Folder"`? It shouldn't be there. Also consider adding quotation marks around the other option: `dir="$1"` – Ted Lyngmo Dec 22 '20 at 03:21
  • @TedLyngmo If I remove the backlash I get `/Users/tim/Desktop/Folder/file.txt` – timgavin Dec 22 '20 at 03:33
  • If you remove the backslash end do `echo "$dir"` - what do you see? – Ted Lyngmo Dec 22 '20 at 03:34
  • 1
    `"~/Desktop/file.txt"` is wrong too. `~` will not be expanded to your home directory if put in quotation marks. – Ted Lyngmo Dec 22 '20 at 03:38
  • `My Folder`. And if I replace the `rsync` command with `echo` I get `-a -v -h -W --progress --compress-level=0 /Users/tim/My Folder/file.txt ~/Desktop/file.txt`. So it all looks great when I echo, but not when I run the command. However, on a Mac that backslash does need to be there – timgavin Dec 22 '20 at 03:40
  • If bash on a Mac requires that backslash, I think that bash version is buggy. The `rsync` line should probably be `rsync "${options[@]}" "/Users/tim/$dir/file.txt" "${HOME}/Desktop/file.txt"` (and no backslash in `dir` - it becomes a part of the filename). Did you also put quotation marks around the other option, making it `dir="$1"`? – Ted Lyngmo Dec 22 '20 at 03:44
  • 1
    @TedLyngmo You're right, I actually didn't need the backslash (I just upgraded to Big Sur) and what you gave fixed it. The problem was the tilde. Adding `${HOME}` fixed it. If you like, post that as an answer and I'll mark it solved. Thank you! – timgavin Dec 22 '20 at 03:50
  • If that's the problem this should be closed as duplicate, not answered. – Charles Duffy Dec 22 '20 at 03:57
  • @CharlesDuffy It had two problems but I guess it's possible to add the other (the backslash thingy) as a duplicate too if it exists. Should I remove the answer? – Ted Lyngmo Dec 22 '20 at 04:02

1 Answers1

0
if [[ $# -eq 0 ]];
then
    dir="My Folder" # this shouldn't have a backslash, it becomes a part of the filename
else
    dir="$1"        # this should have quotation marks around it in case of globbing
fi

The ~ in "~/Desktop/file.txt" will not be expanded to the home directory. Use ${HOME} instead:

rsync "${options[@]}" "/Users/tim/$dir/file.txt" "${HOME}/Desktop/file.txt"
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108