Bash - 使用真实路径将多个文件上传到FTP?

时间:2016-11-21 10:15:29

标签: bash csv file-upload ftp

我有这个脚本将文件上传到FTP(我知道FTP不安全,但客户端坚持使用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/*.csv更改为mput *.csv,那么它可以正常工作(我获得与前一个相同的日志输出,除了它将路径显示为直接位于脚本所在的目录中)。但这只有在我直接从放置目录的脚本中运行脚本时才有效。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

替换

ftp -inv $HOST << EOF

通过

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

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