【问题标题】:Bash - uploading multiple files to FTP using real path?Bash - 使用真实路径将多个文件上传到 FTP?
【发布时间】:2016-11-21 10:15:28
【问题描述】:

我有这个脚本可以将文件上传到 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,那么它可以工作(我得到与前一个相同的日志输出,除了它将路径显示为直接在脚本所在的目录中)。但这只有在我直接从它所在的目录运行脚本时才有效。

有什么想法吗?

【问题讨论】:

    标签: bash csv file-upload ftp


    【解决方案1】:

    替换

    ftp -inv $HOST << EOF
    

    通过

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

    或通过

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

    【讨论】:

    • 看起来应该可以。在连接到 FTP 之前,我使用 cd "${0%/*}" 测试了类似的想法,它可以工作。
    猜你喜欢
    • 2019-12-03
    • 1970-01-01
    • 1970-01-01
    • 2017-07-13
    • 1970-01-01
    • 2012-07-07
    • 2011-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多