2017-01-04 117 views
0

我要自動批處理作業執行以下操作:檢查文件是否在FTP存在與自動bash腳本

  1. 檢查,如果我file.txt的在FTP服務器上存在,我將其重命名到 file.trt
  2. 檢查,如果我file.txt的file.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 

很迷茫,不知道該怎麼做,能有人請幫助我嗎?

+0

我可以用PHP做給你,如果你對它感興趣。你也可以從命令行運行php。 –

+0

不,謝謝:)我找到了一種方法,並在其中工作 – BaFouad

回答

0

我做到這一點,它很好地工作:

#!/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 
相關問題