使用自动 bash 脚本检查 FTP 中是否存在文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41465804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:35:05  来源:igfitidea点击:

Check if a file exist in FTP with an automated bash script

bashftp

提问by BaFouad

I want to automate a batch job that does the following:

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

  1. check if my file.txtexists in a FTP server, I rename it to file.trt
  2. check if my file.txtand file.trtexist, if so I send an email
  3. I run another script
  4. at the end I delete file.trt
  1. 检查我的file.txt 是否存在于 FTP 服务器中,我将其重命名为 file.trt
  2. 检查我的file.txtfile.trt 是否存在,如果存在,我发送电子邮件
  3. 我运行另一个脚本
  4. 最后我删除了file.trt

here's what I have done :

这是我所做的:

#!/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

Am confused and don't know how to do it, can someone please help me?

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

回答by BaFouad

I did this and it work fine :

我这样做了,它工作正常:

#!/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." [email protected] -c [email protected] 

    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