Reputation: 1303
Hey guys I am working on this app that connects to Raspberry Pi through SSH. I use NSTask and NSPipe and shell scripts to execute the shell commands through the Mac app GUI.
The problem is that when I want to copy using "scp" command I get "No such file or directory" error with folders with spaces on its name. If the path is "/Users/home/Desktop/New Files/" doesn't work, but if "/Users/home/Desktop/New Files/" works fine.
The app must be able to use both, separated named folders and not separated, but I just don't know how to do it.
Here is my shell script command:
#!/bin/bash
/usr/bin/ssh [email protected] \
/usr/bin/scp /home/pi/"${1}" [email protected]:/Users/home/Documents/New Files/
The "X"on the ip was intentionally removed and it's not on the real code.
So, anyone could help????
Upvotes: 0
Views: 2268
Reputation: 123470
This is tricky because it requires multiple levels of escaping.
First, start with the actual path:
[email protected]:/Users/home/Documents/New Files/
Now escape it for the shell:
"[email protected]:/Users/home/Documents/New Files/"
Now escape it for scp, since scp accepts shell glob patterns and not filenames:
/usr/bin/scp /home/pi/"${1}" "[email protected]:/Users/home/Documents/New\\ Files/"
Now escape it for ssh, since ssh will evaluate it in a shell:
/usr/bin/ssh [email protected] \
/usr/bin/scp /home/pi/"${1}" \
'"[email protected]:/Users/home/Documents/New\\ Files/"'
This takes care of the "New Files" part, but let's also try to do something about the "${1}" argument:
/usr/bin/ssh [email protected] \
/usr/bin/scp /home/pi/"$(printf "%q" "$1")" \
'"[email protected]:/Users/home/Documents/New\\ Files/"'
Here's a complete, self contained example of the above, going through localhost twice:
#!/bin/bash
cd ~
mkdir "New Files"
touch "my file"
set -- "my file" # Assign $1="my file"
/usr/bin/ssh localhost \
/usr/bin/scp "$(printf "%q" "$1")" \
'"localhost:New\\ Files/"'
find "New Files" -type f
The output shows that it did in fact copy a file with spaces over scp over ssh to the dir with spaces:
New Files/my file
Since multilevel escaping is tricky, if you're having any problems, make sure to try this self contained example first without modifications before concluding that it doesn't work.
Upvotes: 1
Reputation: 2593
Try escaping spaces with a backslash:
[email protected]:/Users/home/Documents/New\ Files/
Upvotes: 0
Reputation: 700
Put "" around the path.
$ mkdir 1 1
mkdir: 1: File exists
$ mkdir "1 1"
$ ll
drwxr-xr-x 2 mikael staff 68 May 21 22:35 1
drwxr-xr-x 2 mikael staff 68 May 21 22:36 1 1
Upvotes: 0