2016-11-21 67 views
0

我有這個腳本上傳文件到FTP(我知道FTP不安全,雖然客戶堅持使用FTP ..)。它工作正常,但它的問題是無法識別上傳時提供的路徑,即使消息說它已成功上傳,但沒有任何內容被上傳。Bash - 使用真實路徑將多個文件上傳到FTP?

所以劇本是這樣的:

#!/bin/bash 

HOST=host 
USER=user   
PASS=pass   
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 


ftp -inv $HOST << EOF 

user $USER $PASS 

get 
cd /path/in/ftp/ 

prompt 
mput $DIR/*.csv 

# End FTP Connection 
bye 

EOF 

rm $DIR/*.csv 

這裏什麼是輸出:

Connected to host. 
220 You have connected to Client's FTP server. 
?Invalid command 
331 User name okay, need password. 
230-Welcome user from ip. You are now logged in to the server. 
230 User logged in, proceed. 
Remote system type is UNIX. 
Using binary mode to transfer files. 
?Invalid command 
250 Directory changed to "/path/in/ftp/" 
?Invalid command 
Interactive mode on. 
mput /path/inv_numbers_2016-11-21_12_09.csv? 200 PORT command successful. 
150 File status okay; about to open data connection. 
226 Closing data connection. Transferred 140 bytes in 1 seconds. 0KB/second. 
140 bytes sent in 0.00 secs (1395.1 kB/s) 
?Invalid command 
221 Session Ended. Downloaded 0KB, Uploaded 1KB. Goodbye user from ip. 

現在,如果我改變mput $DIR/*.csvmput *.csv,然後它(我得到相同的日誌輸出像以前一個,除了它顯示的路徑直接在腳本所在的目錄中)。但是,這隻適用於如果我直接從它所在的目錄運行腳本。

任何想法?

回答

0

通過

cd "$DIR" && ftp -inv $HOST << EOF 

cd "$DIR" || exit 1 
ftp -inv $HOST << EOF 
+0

它看起來這應該更換

ftp -inv $HOST << EOF 

。在連接到FTP之前,我使用'cd「$ {0%/ *}」'測試了類似的想法,並且它工作正常。 – Andrius