Shell项目开发总结
读取配置文件:
source `dirname $0`/ReadIni
设置errorkey(用于错误判断):
set ERROR_KEYS = 'message:|failed|fail|error|overflow|Unexpected|rollback|invalid|not found|incorrect|suspend|denied|failure'
输出到logfile:
echo "===== `date '+%d/%m/%Y %H:%M:%S'` - Generate REPORT_NAME report starting ... ..." | tee -a $LOGFILE
取每个月的第一个工作日:
set GEN_DAY = `cal $MON $YEAR | awk 'NR<3{next} {if(NF==1){next;print $1;exit} if(NF<7){ print $1;exit} print $2;exit}'`
set FIRST_WORKING_DAY = `echo ${YEAR}${MON}0${GEN_DAY}`
shell中执行sql:
echo "Truncate table I_EXCEPT_RPT." | tee -a $LOGFILE
runsql << EOF_CLR_DATA >! $TMP_EXTRACT_FILE
set nocount on
go
truncate table EXCEPT_RPT
go
EOF_CLR_DATA
if ("`egrep -l -i '$ERROR_KEYS' $TMP_EXTRACT_FILE`" == '') then
echo "Truncate the data in EXCEPT_RPT succeed" | tee -a $LOGFILE
else
echo "Truncate the data in EXCEPT_RPT failed" | tee -a $LOGFILE
cat $TMP_EXTRACT_FILE >> $LOGFILE
set RETURN_VALUE = 1
goto eoj_skip
endif
执行存储过程:
runsql << GEN_RPT >! $TMP_RPT_FILE
set nocount on
go
exec sp_gen_xx_except_rpt '${REPORT_FREQUENCY}','${GENERATION_DATE}'
go
GEN_RPT
用sed替换,删除:
sed 's/^[ ]*//' $TMP_EXTRACT_FILE | sed '/---/d' | sed '/return status/d' > $MAIL_CONTENT
插入header:
sed "1 i\\
`sed '/---/d' $TMP_EXTRACT_FILE | sed -n '2p' | sed 's/^[ ]*//g'`" $DAT/$FNAME > /tmp/RPT.tmp
从数据库中导出data(sybase bcp):
# bcp out the view on EXCEPT_RPT to a tab-delimited file
$SYB/bin/bcp $DB..$BCP_VIEW out $DAT/$FNAME -U$NAME -P$PASSWD -S$SERVER -c >>! $DAT/$FNAME.bcpout
把文件数据导入sybase数据库:
#文件格式为:“|” 分割 #如:xxxx|dddd|1111 $SYB/bin/bcp $DBNAME..${TABLENAME}_UP in $WRK/$RCS_FILENAME -U$LOGIN -P$PASSWD -S$DSQUERY -t'|' -c -b1000 >! $WRK/$FILENAME.bcpin.$TMSTMP
发送email:
uuencode $DAT/$FNAME $FNAME | cat $MAIL_CONTENT - | mailx -s "${MAIL_SUBJECT}" $MAIL_LIST
echo "$REPORT_NAME report has been sent to $MAIL_LIST. " | tee -a $LOGFILE
Clean up Logs:
echo "" | tee -a $LOGFILE
echo "Cleaning up the LOG files created 14 days ago ..." | tee -a $LOGFILE
find $LOG/${REPORT_NAME}* -mtime +14 -type f -exec rm {} \;
echo "Cleaning up the Report files created 30 days ago ..." | tee -a $LOGFILE
find $DAT/*$FNAME* -mtime +30 -type f -exec rm {} \;
Exception handle:
eoj_skip:
echo "" | tee -a $LOGFILE
echo "===== `date '+%d/%m/%Y %H:%M:%S'` - Generate $REPORT_NAME report end" | tee -a $LOGFILE
exit $RETURN_VALUE
待续中。。。。
打印执行时间:
echo "" | tee -a $LOGFILE echo "*****************************************" | tee -a $LOGFILE echo "DONE : $0 $*" | tee -a $LOGFILE echo "-----------" | tee -a $LOGFILE echo "Start time : $startime" | tee -a $LOGFILE echo "Finish time : `date '+%D %T' ` " | tee -a $LOGFILE