#current work directory CW='/path/to/work/space' YESTERDAY=\`date +%Y%m%d -d 'yesterday'\` if \[ $1 \]; then YESTERDAY=$1 fi
download () { if \[ -f "Statement/zip/$YESTERDAY.Statement.zip" \]; then echo"Already sending $YESTERDAY.statement" | tee -a $LF read -n1 -p "Do you want force to send mail \[Y|N\]? " current_input case$current_inputin Y|y) echo"Downloading ${YESTERDAY}_zhye-yh-cqg.zip file" | tee -a $LF curl "http://www.someurl.com/files" ;; N|n) echo'Choice no. exit' | tee -a $LF ;; esac else if \[ ! -f "download/$YESTERDAY/$YESTERDAY"_zhye-yh-cqg.zip \];then echo"Downloading ${YESTERDAY}_zhye-yh-cqg.zip file" | tee -a $LF curl "http://www.someurl.com/files" else echo"Found ${YESTERDAY}_*.zip zips will not download..." | tee -a $LF fi fi }
makedirs() { if \[ ! -x "Statement/utf8/$YESTERDAY" \]; then echo"Making dirs utf8/$YESTERDAY" | tee -a $LF mkdir -p "Statement/utf8/$YESTERDAY/" else echo"dir Statement/utf8/$YESTERDAY existed." |tee -a $LF fi
if \[ ! -x "Statement/zip/" \]; then echo"Making dir zip" | tee -a $LF mkdir -p "Statement/zip/" else echo'dir Statement/zip/ existed.' | tee -a $LF fi
if \[ ! -x "Statement/GBK/$YESTERDAY" \]; then echo"Making dirs GBK/$YESTERDAY" | tee -a $LF mkdir -p "Statement/GBK/$YESTERDAY/" else echo"dir Statement/GBK/$YESTERDAY existed." | tee -a $LF fi }
unzipFiles() { targetFile=\`ls Statement/GBK/$YESTERDAY/ | wc -l\` if \[ "$targetFile" -ge '1' \];then echo"GBK/$YESTERDAY/*.csv found, will not unzip file" else if \[ -f "download/$YESTERDAY/${YESTERDAY}_zhye-yh-cqg.zip" \];then echo"Finding *.zip then unzip them"| tee -a $LF find download -name "*.zip" -exec unzip -o -d Statement/GBK/$YESTERDAY {} \\; else echo"Zip file download/$YESTERDAY/${YESTERDAY}_zhye-yh-cqg.zip no found. Download failed" | tee -a $LF fi fi } clearFiles() { echo"Clear up...*.zip"| tee -a $LF find download -name "*.zip" -execrm -f {} \\; }
transaferFiles() { targetFile=\`ls Statement/GBK/$YESTERDAY/ | wc -l\` if \[ "$targetFile" -ge '1' \];then echo"Encoding start..." | tee -a $LF files=\`find Statement/GBK/$YESTERDAY -name "*.csv"\` echo"iconf GBK to UTF8" | tee -a $LF for file in$files do filename=\`basename$file\` iconv -f GBK -t UTF-8 -c $file > Statement/utf8/$YESTERDAY/$filename done else echo"GBK/$YESTERDAY/$YESTERDAY/.csv files no found" | tee -a $LF errorReport fi
}
zipFiles() { if \[ ! -f "Statement/zip/$YESTERDAY.Statement.zip" \]; then echo"Zipping files into $YESTERDAY.Statement.zip" | tee -a $LF zip Statement/zip/$YESTERDAY.Statement.zip Statement/utf8/$YESTERDAY/* else echo"Found zip file will not zip them" | tee -a $LF fi }
sendingFiles() { echo"Sending $YESTERDAY.Statement.zip to someone and simon" | tee -a $LF echo"$YESTERDAY 对账单已发送" |mail -s "Statements for $YESTERDAY" -a Statement/zip/$YESTERDAY.Statement.zip -c someone@address.com me@email.com }
sendingSingleFile() { targetFile=\`find Statement/utf8/${YESTERDAY}/ -name "${YESTERDAY}_jymx-zjtg*"\` echo"$YESTERDAY 对账单已发送" |mail -s "Statements for $YESTERDAY" -a $targetFile -c someone@address.com me@email.com } errorReport(){ echo"对账单发送错误了快查看" |mail -s "ERROR:Statements sending error" -a /var/log/statement/daily/statement.log alert@email.com echo"对账单发送错误" | tee -a $LF echo'-------------------------------' >> $LF exit 1 } main() { echo'Start... '\`date +%Y%m%d-%H:%M:%S\` | tee -a $LF echo"Moving to $CW" | tee -a $LF cd$CW download makedirs unzipFiles transaferFiles zipFiles if \[ ! -f "Statement/zip/$YESTERDAY.Statement.zip" \]; then errorReport else #sendingFiles sendingSingleFile clearFiles fi echo'Done...'\`date +%Y%m%d-%H:%M:%S\` >> $LF echo'-------------------------------' >> $LF }