0
我已經寫了一個shell腳本來將遠程文件ftp到本地服務器。但它給我錯誤。 腳本:從遠程到本地的ftp最新文件
#!/bin/sh
cd /home/local-path
HOST='hostname'
USER='username'
PASSWD='password'
ftp -n -v $HOST << EOT
binary
user $USER $PASSWD
cd /home/server-path
theFile=$(ls -t | head -n 1)
get $theFile
bye
EOT
當運行我得到以下錯誤:
Connected to server.
220 (vsFTPd 2.0.5)
530 Please login with USER and PASS.
331 Please specify the password.
230 Login successful.
250 Directory successfully changed.
?Invalid command
(remote-file) (local-file)
221 Goodbye.
如果我硬編碼完整的文件路徑,而不是CD /家庭/服務器的路徑 & theFile = $(LS -t | head -n 1),它工作正常。問題是我不知道確切的遠程文件名,因爲有多個文件,我只需要ftp最新的一個。
您能否請回顧並告訴我我在這裏做了什麼錯誤,謝謝。
'theFile = $(LS -t |頭-n 1)'是不是有效的FTP語法。這是'bash'語法,完全不同。最簡單的解決方案可能是建立兩個連接 - 一個用來運行'ls -t',這樣你就可以解析會話的輸出,另一個可以解析出你所解析的文件。或者,你可以運行FTP作爲協處理器,但是這更復雜和脆弱,並且很難正確得到... – twalberg
ls -t不是有效的ftp命令。你可以參考這個鏈接的答案https://stackoverflow.com/questions/28835893/get-the-latest-file-from-a-remote-server-from-an-ftp-in-unix –