【问题标题】:Check if a file exist in FTP with an automated bash script使用自动 bash 脚本检查 FTP 中是否存在文件
【发布时间】:2017-01-04 14:08:26
【问题描述】:

我想自动化执行以下操作的批处理作业:

  1. 检查我的 file.txt 是否存在于 FTP 服务器中,我将其重命名为 file.trt
  2. 检查我的 file.txtfile.trt 是否存在,如果存在我发送电子邮件
  3. 我运行另一个脚本
  4. 最后我删除了 file.trt

这是我所做的:

#!/bin/bash
host='ftp.xxxx.com'
USER='xxxx'
PASSWD='xxxx'

ftp -n -v $host << EOF
ascii
user $USER $PASSWD
prompt
mls /TEST/file.txt test.txt
quit
EOF

if [[ $? -eq 0 ]] 
        then
            echo "The File file.txt Exists";
        else 
            echo "The File file.txt dons not Exist";
fi

我很困惑,不知道该怎么做,有人可以帮我吗?

【问题讨论】:

  • 如果您对 PHP 感兴趣,我可以使用 PHP 为您完成。您也可以从命令行运行 php。
  • 不,谢谢 :) 我找到了一种方法并正在努力

标签: bash ftp


【解决方案1】:

我这样做了,效果很好:

#!/bin/bash
host='ftp.xxxx.com'
USER='xxxx'
PASSWD='xxxx'

FTPFile1="/TEST/file.txt"
FTPFile2="/TEST/file.trt"


#search file.txt et file.trt on the ftp server
ftp -n -v $host << EOT
ascii
user $USER $PASSWD
prompt
mls $FTPFile1 $FTPFile2 test.txt
quit
EOT

# # ----------------------------------------------------------------------------------------------------------------------# 
# # test if file.txt et file.trt are present => send mail
# # ----------------------------------------------------------------------------------------------------------------------#

if grep -inr '/TEST/file.txt' test.txt && grep -inr '/TEST/file.trt' test.txt
    then
        echo "" | mail -s "aaaaa] Error, aaaaa." mail@mail.fr -c mail@mail.fr 

    elif grep -inr '/TEST/file.txt' test.txt 
        then
        echo "The File file.trt does not Exist (no loading at the moment), rename file.txt to file.trt and start loading";

    # ----------------------------------------------------------------------------------------------------------------------#       
    # rename file.txt 
    #  ----------------------------------------------------------------------------------------------------------------------#

ftp -n -v $host<< EOT
    user $USER $PASSWD
    get $FTPFile1 file.txt
    rename $FTPFile1 $FTPFile2 
    quit
EOT


    else
        echo " file.txt (file not yet ready). No action to do."     
fi

# ------------------------------------#     

    ftp -n -v $host << EOT
    user $USER $PASSWD
    get $FTPFile1 file.txt
    quit
EOT

    TRT_DATE=`cat file.txt | grep -Po "[0-9]{8}"`


        # run my_script.sh  
              ./my_script.sh -d $TRT_DATE


#delete file.txt et test.txt 
rm -f file.txt test.txt

# delete file.trt
ftp -n -v $host << EOT
    user $USER $PASSWD
    delete $FTPFile2 
    quit
EOT

exit 0

【讨论】:

    猜你喜欢
    • 2023-01-20
    • 2015-01-29
    • 1970-01-01
    • 2014-08-23
    • 1970-01-01
    • 2013-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多