scp 遠端檔案到本機

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'

更新:括號的問題...

答案1

您需要轉義每個空格和括號:

#!/bin/bash

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

順便說一下,一個更友善的選擇是$aaa除了雙引號之外還用單引號引起來:

#!/bin/bash

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

答案2

下面為我工作。我認為你只需要轉義空格、括號或其他任何東西就可以了。

#!/bin/bash

aaa="/tmp/untitled\ text\ 2.txt"

scp -r [email protected]:"$aaa" .

答案3

我在遠端主機上建立了一個文件,其字面名稱為“”/tmp/some file withspaces(and括號).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

帶變數

/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

相關內容