直接上代码

#!/bin/bash
HOST="host"
# 从远处服务器下载文件
PASS="passwd"
basePath="/home/dev/"
#FTP账号
USRNAME="username";

function down_file {
    echo "yest1:$yest1, yest2:$yest2, fileName:$fileName"

/usr/bin/expect <<EOD
spawn /usr/bin/ftp -n $HOST
set timeout 500
expect {
	"*Welcome" { send "user $USRNAME $PASS \r" }
}

expect {
	"*OK" { send "cd build \r prompt \r mget $fileName $filePath \r" }
}
expect "ftp>" #证明下载完毕
send "bye \r"
expect eof
EOD

}

echo -e "$(date "+%Y-%m-%d %H:%M:%S") down file start"

# 下载近3天的文件
for((i=3;i>=1;i-=1))
do
    month=`date -d "$i day ago" +%Y%m`
    yest1=`date -d "$i day ago" +%Y%m%d`
    yest2=`date -d "$i day ago" +%y%m%d`
    #yest2=20210913
    fileName="${USRNAME}_${yest1}_*.xls"
    fileNameChkExist="${USRNAME}_${yest1}_1.xls"
    filePath="${basePath}${month}/"
    fileNameLocal=$filePath$fileName
    cd $filePath
    if [ ! -d $filePath ]; then
        mkdir -p $filePath
        # sudo mkdir -p -m 755 $filePath
        echo "sudo mkdir -p ${filePath} done"
    fi
    if [ ! -f "$filePath$fileNameChkExist" ]; then
        down_file
    else
        echo "$yest1 file exists"
    fi

done

echo -e "$(date "+%Y-%m-%d %H:%M:%S") down file end\n"