使用Bash shell的FTP登录脚本

时间:2014-01-02 21:36:55

标签: bash shell ftp file-transfer

我编写了这个脚本来登录FTP服务器并将文件从本地目录放到FTP服务器上。这最终将用于通过crontab自动传输文件。我收到一个问题,说它不是连接,文件永远不会被转移。有什么可以帮我弄清楚发生了什么事吗?代码和错误列在下面。

#!/bin/bash

extention=".txt"
filename="file.txt"
counter="0"
filesize=$(stat -c %s $filename)
ftp_file="new_file"`date -d yesterday +"%Y%m%d"`"$extention"

while true; do
    if [[ $filesize -gt 0 ]]; then
        oldsize=$(echo $filesize)
        filesize=$(stat -c %s $filename)
        if [[ $oldsize == $filesize ]]; then
            cat $filename > $ftp_file
            ####FTP command
                HOST='host'  #This is the FTP servers host or IP address.
                USER='user'       #This is the FTP user that has access to the server.
                PASS='pass'    #This is the password for the FTP user.
                echo $HOST
                echo $USER
                echo $PASS
                ftp -n $HOST <<-EOF
                quote USER $USER
                quote PASS $PASS
                cd /directory
                put $ftp_file
                quit
                EOF
            ####Archive file
            mv $ftp_file archive/.
            rm $filename
            exit 0
        else
            echo "File is still processing. size of file is: $filesize"
            echo "$oldsize"
        fi
    else echo "Size of file is 0"
        if [[ $counter -gt 5 ]]; then
        exit 0
        fi
    fi
sleep 5
counter=$counter+1
done

当我运行脚本来测试它时,我收到了这些错误。

$ ~
$ ./ftp.sh
host
user
pass
Not connected.
Not connected.
Not connected.
Not connected.
> ftp: connect :Unknown error number

我环顾四周并尝试了不同的东西,但我一直没有得到这些错误。任何帮助将不胜感激!

谢谢, 莱恩

0 个答案:

没有答案