一直以来对shell都不是很熟悉,只停留在基本的linux上操作上,这周因为定位问题接触到了生产环境上的脚本,因此作为引子学习一下。很多命令只是点到,等真正需要独立完成的时候再去学习。
#!/bin/sh #set -x 打印出调试信息
MYNAME=rawload
eamid=$
emsentityid=$
WORK_DIR=${PM4H_APP}/module/rawload
LOAD_DIR=${PM4H_SPOOL}/${eamid}/${emsentityid}/load
LOAD_BACKUP_DIR=${PM4H_SPOOL}/${eamid}/${emsentityid}/load_backup
FILELIST=${PM4H_SPOOL}/${eamid}/${emsentityid}/etc/.rawloadfilelist #调用返回sql执行结果
SQLEXECCMD="$JAVA -jar $NETWATCHER_HOME/pm4h2/app/lib/sqlexec.jar" PRESENTLIST=${PM4H_SPOOL}/${eamid}/${emsentityid}/etc/.presentlist
LASTLIST=${PM4H_SPOOL}/${eamid}/${emsentityid}/etc/.lastlist
CFORMATLIST=${PM4H_SPOOL}/${eamid}/${emsentityid}/etc/.cformatlist
CDELETELIST=${PM4H_SPOOL}/${eamid}/${emsentityid}/etc/.cdeletelist
FORMATEDLIST=${PM4H_SPOOL}/${eamid}/${emsentityid}/etc/.formatedlist pidfile=.$MYNAME.$eamid.$emsentityid.pid #test -f 命令可以判断文件是否存在
if test -f ${WORK_DIR}/${pidfile}
then
# See if a process is running with that process id
pid=`cat $WORK_DIR/$pidfile` # test -n 字符串的长度非0
if test -n "$pid"
then
# 这句话的意思是:查看所有包含有$MYNAME关键字的进程,并且去掉grep本身进程,将输出结果按照空格分割,并打印如第二个参数,并从中查询包含了$pid的记录,输出到/dev/null(废弃)
ps -ef|grep $MYNAME|grep -v grep|$AWK '{print $2}'|grep $pid > /dev/null
# $? 表示上一次命令执行的结果值,测试上一次执行结果是否为0,0表示匹配成功,1表示失败
if test $? -eq
then
# The process is running !
echo "The process is running !"
exit
else
# Try the ps listing again as it is not always reliable
#ps -ef|grep $pid|grep $MYNAME > /dev/null
ps -ef|grep $MYNAME|grep -v grep|$AWK '{print $2}'|grep $pid > /dev/null
if test $? -eq
then
# The process is running !
echo "The process is running !"
exit
fi
fi
fi
fi # 将当前运行此脚本的进程号写入$pidfile
echo $$ > $WORK_DIR/$pidfile
# Check process is existence
if [ ! -f ${WORK_DIR}/rawload.jar ] ; then
printf "Error: Can not find the file ${WORK_DIR}/rawload.jar\n"
return
fi #每次处理个数默认是1000,数据库中的配置
pifcounter="" #get pif files' number to batch load
sql="select t.parametervalue from pm4h_ad.cfg_sysparameter t where t.parametermoduleid='BG_RAWLOAD' and t.parameterid='${eamid}_PIFNUM'" echo $sql > ${WORK_DIR}/.getpifnum.sql
# 执行SQL,因无法直接获取到查询结果,故将结果写入到.pifnumresult中
$SQLEXECCMD ${WORK_DIR}/.getpifnum.sql "$ORACLE_IP;$ORACLE_PORT;$ORACLE_SID;$ORACLE_ADUSER;$ORACLE_ADPWD" > ${WORK_DIR}/.pifnumresult
# 如果上面的shell执行成功,则获取
if [ $? -eq ]; then
# 使用|分割行,并取第二行,打印出第一部分,NR表示读取的文件行数;FNR表示读取到的当前文件的行数,NR==FNR常用语判断输入多个文件时读取到第一个文件
pifcounter=`$AWK -F\| '{if(NR==2){print $1}}' ${WORK_DIR}/.pifnumresult`
fi
if [ "$pifcounter" = "" ]; then
# 如果针对某一ems实体的配置为空,则获取全局ems配置
sql="select t.parametervalue from pm4h_ad.cfg_sysparameter t where t.parametermoduleid='BG_RAWLOAD' and t.parameterid='PIFNUM'"
echo $sql > ${WORK_DIR}/.getpifnum.sql
$SQLEXECCMD ${WORK_DIR}/.getpifnum.sql "$ORACLE_IP;$ORACLE_PORT;$ORACLE_SID;$ORACLE_ADUSER;$ORACLE_ADPWD" > ${WORK_DIR}/.pifnumresult
if [ $? -eq ]; then
pifcounter=`$AWK -F\| '{if(NR==2){print $1}}' ${WORK_DIR}/.pifnumresult`
fi
if [ "$pifcounter" = "" ];then
# 如果上面的都失败了,则插入该记录到数据库,并设置默认值1000
sql="insert into pm4h_ad.cfg_sysparameter(PARAMETERMODULEID,PARAMETERID,PARAMETERNAME,PARAMETERVALUE,DESCRIPTION) values('BG_RAWLOAD','PIFNUM','PIFNUM',1000,'the number of pif files which are handled by one process');"
echo $sql > ${WORK_DIR}/.insertpifnum.sql
$SQLEXECCMD ${WORK_DIR}/.insertpifnum.sql "$ORACLE_IP;$ORACLE_PORT;$ORACLE_SID;$ORACLE_ADUSER;$ORACLE_ADPWD"
pifcounter=
fi
fi # 获取是否加载历史数据配置
sql="select t.parametervalue from pm4h_ad.cfg_sysparameter t where t.parametermoduleid='BG_ALL' and t.parameterid='DATAPRIORITY'"
echo $sql > ${WORK_DIR}/.selecthistorydate.sql
$SQLEXECCMD ${WORK_DIR}/.selecthistorydate.sql "$ORACLE_IP;$ORACLE_PORT;$ORACLE_SID;$ORACLE_ADUSER;$ORACLE_ADPWD" > ${WORK_DIR}/.ishistorydataresult if [ $? -eq ];then
ishistorydata=`$AWK -F\| '{if(NR==2){print $1}}' ${WORK_DIR}/.ishistorydataresult`
else
ishistorydata=
fi if [ "$ishistorydata" = "" ];then
ishistorydata=
fi # 将文件名补充为完整的路径并追加到${PRESENTLIST}, {s:old:new:g}, ^表示匹配行开始,$表示匹配行结尾;pif$表示匹配”pif“且字段必须在结尾
ls ${LOAD_DIR}|sed "s:^:${LOAD_DIR}/:"|grep "\.pif$" > ${PRESENTLIST}
# 如果存在${LASTLIST}并且不存在${FORMATEDLIST},那么删除${LASTLIST};-a在test中表示and,&&仅当第一个判断为真时才会执行
test -f ${LASTLIST} -a ! -f ${FORMATEDLIST} && rm -f ${LASTLIST}
# 如果存在${FORMATEDLIST}而且不存在${LASTLIST},那么将${FORMATEDLIST}中的内容使用|分割后,取第三个参数赋值到${LASTLIST}
test -f ${FORMATEDLIST} -a ! -f ${LASTLIST} && awk -F"|" '{print $3 > "'${LASTLIST}'"}' ${FORMATEDLIST}
# 如果${FORMATEDLIST}不存在则新建${FORMATEDLIST}
test -f ${FORMATEDLIST} || touch ${FORMATEDLIST}
# 如果${LASTLIST}不存在则新建
test -f ${LASTLIST} || touch ${LASTLIST}
# 将${LASTLIST}中的内容升序排列并重新输出到${LASTLIST}
sort ${LASTLIST} -o ${LASTLIST}
# 取在${PRESENTLIST}出现但未在${LASTLIST}出现的记录到${CFORMATLIST},也就是新增的需要入库的文件
comm - ${PRESENTLIST} ${LASTLIST} > ${CFORMATLIST}
# 取在${LASTLIST}中,但不在${PRESENTLIST}中的文件列表,也就是需要删除的文件
comm - ${LASTLIST} ${PRESENTLIST} > ${CDELETELIST} # 如果${CDELETELIST}存在且可以被读取,那么执行SynLoadFmts这个jar,主要逻辑是从FORMATEDLIST和LASTLIST删除掉CDELETELIST中的内容
test -s ${CDELETELIST} && $JAVA ${JVM_PARAMETER} -jar -Xmx512m ${WORK_DIR}/SynLoadFmts.jar ${FORMATEDLIST} ${LASTLIST} ${CDELETELIST} ${emsentityid}
# 将 ${CFORMATLIST}中的内容解析并输出到${FORMATEDLIST}
test -s ${CFORMATLIST} && cat ${CFORMATLIST}|xargs -i awk -F"|" '{if(NR==1){for(i=1;i<NF;i++){if($i=="STARTDAY"){j=i;field2=$(i-2);}}}if(NR==3){field3=$j$(j+1);field4=$(j+2);exit;}}END{if(FNR<3){cmd=sprintf("mv %s %s", FILENAME, "'${LOAD_BACKUP_DIR}'");system(cmd);}else{print field3"|"field2"|"FILENAME"|"field4 >> "'${FORMATEDLIST}'";print FILENAME >> "'${LASTLIST}'"}}' {}
if [ "$ishistorydata" = "" ];then
cat ${FORMATEDLIST}|grep -v '\.re[a-z0-9]\{8\}-[a-z0-9]\{4\}-[a-z0-9]\{4\}-[a-z0-9]\{4\}-[a-z0-9]\{12\}\.pif'|sort |head -n $pifcounter > $FILELIST
#取$FILELIST的行数
fileline=`cat $FILELIST|wc -l`
#如果要采集的正常文件不足1000,则进行补采
if [ $fileline -lt $pifcounter ];then
addfilenumber=`expr $pifcounter \- $fileline`
cat ${FORMATEDLIST}|grep '\.re[a-z0-9]\{8\}-[a-z0-9]\{4\}-[a-z0-9]\{4\}-[a-z0-9]\{4\}-[a-z0-9]\{12\}\.pif'|sort |head -n $addfilenumber >> $FILELIST fi
else
#history data
cat ${FORMATEDLIST}|grep '\.re[a-z0-9]\{8\}-[a-z0-9]\{4\}-[a-z0-9]\{4\}-[a-z0-9]\{4\}-[a-z0-9]\{12\}\.pif'|sort |head -n $pifcounter > $FILELIST fileline=`cat $FILELIST|wc -l`
if [ $fileline -lt $pifcounter ];then
addfilenumber=`expr $pifcounter \- $fileline`
cat ${FORMATEDLIST}|grep -v '\.re[a-z0-9]\{8\}-[a-z0-9]\{4\}-[a-z0-9]\{4\}-[a-z0-9]\{4\}-[a-z0-9]\{12\}\.pif'|sort |head -n $addfilenumber >> $FILELIST fi
fi $JAVA `echo ${JVM_PARAMETER}|sed 's/'-DORACLE_SID=$ORACLE_SID'/'-DORACLE_SID=$ORACLE_SID1'/'` -cp ${CLASSPATH} -jar ${WORK_DIR}/rawload.jar $LOAD_DIR $LOAD_BACKUP_DIR ${emsentityid} ${FILELIST}
rm -f ${WORK_DIR}/$pidfile
rm -f ${WORK_DIR}/.getpifnum.sql
rm -f ${WORK_DIR}/.pifnumresult
rm -f ${WORK_DIR}/.insertpifnum.sql
rm -f ${WORK_DIR}/.selecthistorydate.sql
rm -f ${WORK_DIR}/.ishistorydataresult
rm -f ${WORK_DIR}/.emptyfile
test -f ${PRESENTLIST} && rm -f ${PRESENTLIST}
test -f ${CFORMATLIST} && rm -f ${CFORMATLIST}
test -f ${CDELETELIST} && rm -f ${CDELETELIST}
#echo -e "End:\t`date +%Y-%m-%d" "%H:%M:%S`" >> ${PM4H_SPOOL}/${eamid}/${emsentityid}/etc/time
#set +x
exit