scp远程文件到本地机器

作为脚本的一部分,我试图从远程站点复制文件。 但有一个错误。 对我来说,听起来有点奇怪,

#aaa="/path/to/some file with spaces(and brackets).txt" .... #scp [email protected]:"$aaa" /test/ bash: -c: line 0: syntax error near unexpected token `(' bash: -c: line 0: `scp -f /path/to/some file with spaces.txt' 

upd:括号中的问题…

您需要转义每个空格和括号:

 #!/bin/bash aaa='/path/to/some\ file\ with\ spaces\(and brackets\).txt' scp [email protected]:"$aaa" /test/ 

顺便说一句,一个更友好的select是除了双引号之外,用单引号括起$aaa

 #!/bin/bash aaa='/path/to/some file with spaces(and brackets).txt' scp [email protected]:"'$aaa'" /test/ 

下面为我​​工作。 我想你只需要逃离空间,括号或其他任何东西,你应该是好的。

 #!/bin/bash aaa="/tmp/untitled\ text\ 2.txt" scp -r [email protected]:"$aaa" . 

我在远程主机上创build了一个文件,文件名是`“/ tmp /一些带空格(和括号).txt〜的文件。

如果你加倍+单引号这样的名字,所以我能够转移它。 受这个问题的启发。

 /tmp$ scp remotehost:"'/tmp/some file with spaces(and brackets).txt'" . some file with spaces(and brackets).txt 100% 0 0.0KB/s 00:00 

随着variables

 /tmp$ aaa="/tmp/some file with spaces(and brackets).txt" /tmp$ scp puppet:"'$aaa'" . some file with spaces(and brackets).txt 100% 0 0.0KB/s 00:00