• 生产环境上shell的解读


    一直以来对shell都不是很熟悉,只停留在基本的linux上操作上,这周因为定位问题接触到了生产环境上的脚本,因此作为引子学习一下。很多命令只是点到,等真正需要独立完成的时候再去学习。

      1 #!/bin/sh
      2 
      3 #set -x 打印出调试信息
      4 MYNAME=rawload
      5 eamid=$2
      6 emsentityid=$1
      7 WORK_DIR=${PM4H_APP}/module/rawload
      8 LOAD_DIR=${PM4H_SPOOL}/${eamid}/${emsentityid}/load
      9 LOAD_BACKUP_DIR=${PM4H_SPOOL}/${eamid}/${emsentityid}/load_backup
     10 FILELIST=${PM4H_SPOOL}/${eamid}/${emsentityid}/etc/.rawloadfilelist
     11 
     12 #调用返回sql执行结果
     13 SQLEXECCMD="$JAVA -jar $NETWATCHER_HOME/pm4h2/app/lib/sqlexec.jar"
     14 
     15 16 PRESENTLIST=${PM4H_SPOOL}/${eamid}/${emsentityid}/etc/.presentlist
     17 LASTLIST=${PM4H_SPOOL}/${eamid}/${emsentityid}/etc/.lastlist
     18 CFORMATLIST=${PM4H_SPOOL}/${eamid}/${emsentityid}/etc/.cformatlist
     19 CDELETELIST=${PM4H_SPOOL}/${eamid}/${emsentityid}/etc/.cdeletelist
     20 FORMATEDLIST=${PM4H_SPOOL}/${eamid}/${emsentityid}/etc/.formatedlist
     21 
     22 pidfile=.$MYNAME.$eamid.$emsentityid.pid
     23 
     24 #test -f 命令可以判断文件是否存在
     25 if test -f ${WORK_DIR}/${pidfile}
     26 then
     27   # See if a process is running with that process id
     28   pid=`cat $WORK_DIR/$pidfile`
     29   
     30   # test -n 字符串的长度非0
     31   if test -n "$pid"
     32   then
     33      # 这句话的意思是:查看所有包含有$MYNAME关键字的进程,并且去掉grep本身进程,将输出结果按照空格分割,并打印如第二个参数,并从中查询包含了$pid的记录,输出到/dev/null(废弃)
     34      ps -ef|grep $MYNAME|grep -v grep|$AWK '{print $2}'|grep $pid > /dev/null
     35      # $? 表示上一次命令执行的结果值,测试上一次执行结果是否为0,0表示匹配成功,1表示失败
     36      if test $? -eq 0
     37      then
     38         # The process is running !
     39         echo "The process is running !"
     40         exit 3
     41      else
     42        # Try the ps listing again as it is not always reliable
     43        #ps -ef|grep $pid|grep $MYNAME > /dev/null
     44        ps -ef|grep $MYNAME|grep -v grep|$AWK '{print $2}'|grep $pid > /dev/null
     45        if test $? -eq 0
     46        then
     47             # The process is running !
     48             echo "The process is running !"
     49             exit 3
     50        fi
     51      fi
     52   fi
     53 fi
     54 
     55 # 将当前运行此脚本的进程号写入$pidfile
     56 echo $$ > $WORK_DIR/$pidfile
     57 # Check process is existence
     58 if [ ! -f ${WORK_DIR}/rawload.jar ] ; then
     59     printf "Error: Can not find the file ${WORK_DIR}/rawload.jar
    "
     60     return 2
     61 fi
     62 
     63 #每次处理个数默认是1000,数据库中的配置
     64 pifcounter=""
     65 
     66 #get pif files' number to batch load
     67 sql="select t.parametervalue from pm4h_ad.cfg_sysparameter t where t.parametermoduleid='BG_RAWLOAD' and t.parameterid='${eamid}_PIFNUM'"
     68 
     69 echo $sql > ${WORK_DIR}/.getpifnum.sql
     70 # 执行SQL,因无法直接获取到查询结果,故将结果写入到.pifnumresult中
     71 $SQLEXECCMD ${WORK_DIR}/.getpifnum.sql 1 "$ORACLE_IP;$ORACLE_PORT;$ORACLE_SID;$ORACLE_ADUSER;$ORACLE_ADPWD" > ${WORK_DIR}/.pifnumresult
     72 # 如果上面的shell执行成功,则获取
     73 if [ $? -eq 0 ]; then
     74     # 使用|分割行,并取第二行,打印出第一部分,NR表示读取的文件行数;FNR表示读取到的当前文件的行数,NR==FNR常用语判断输入多个文件时读取到第一个文件
     75     pifcounter=`$AWK -F| '{if(NR==2){print $1}}' ${WORK_DIR}/.pifnumresult`
     76 fi
     77 if [ "$pifcounter" = "" ]; then
     78     # 如果针对某一ems实体的配置为空,则获取全局ems配置
     79     sql="select t.parametervalue from pm4h_ad.cfg_sysparameter t where t.parametermoduleid='BG_RAWLOAD' and t.parameterid='PIFNUM'"
     80     echo $sql > ${WORK_DIR}/.getpifnum.sql
     81     $SQLEXECCMD ${WORK_DIR}/.getpifnum.sql 1 "$ORACLE_IP;$ORACLE_PORT;$ORACLE_SID;$ORACLE_ADUSER;$ORACLE_ADPWD" > ${WORK_DIR}/.pifnumresult
     82     if [ $? -eq 0 ]; then
     83         pifcounter=`$AWK -F| '{if(NR==2){print $1}}' ${WORK_DIR}/.pifnumresult`
     84     fi
     85     if [ "$pifcounter" = "" ];then
     86         # 如果上面的都失败了,则插入该记录到数据库,并设置默认值1000
     87         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');"
     88         echo $sql > ${WORK_DIR}/.insertpifnum.sql
     89         $SQLEXECCMD ${WORK_DIR}/.insertpifnum.sql 1 "$ORACLE_IP;$ORACLE_PORT;$ORACLE_SID;$ORACLE_ADUSER;$ORACLE_ADPWD" 
     90         pifcounter=1000
     91     fi
     92 fi
     93 
     94 # 获取是否加载历史数据配置
     95 sql="select t.parametervalue from pm4h_ad.cfg_sysparameter t where t.parametermoduleid='BG_ALL' and t.parameterid='DATAPRIORITY'"
     96 echo $sql > ${WORK_DIR}/.selecthistorydate.sql
     97 $SQLEXECCMD ${WORK_DIR}/.selecthistorydate.sql 1 "$ORACLE_IP;$ORACLE_PORT;$ORACLE_SID;$ORACLE_ADUSER;$ORACLE_ADPWD" > ${WORK_DIR}/.ishistorydataresult
     98 
     99 if [ $? -eq 0 ];then
    100     ishistorydata=`$AWK -F| '{if(NR==2){print $1}}' ${WORK_DIR}/.ishistorydataresult`
    101 else
    102     ishistorydata=0
    103 fi
    104 
    105 if [ "$ishistorydata" = "" ];then
    106     ishistorydata=0
    107 fi
    108 
    109 # 将文件名补充为完整的路径并追加到${PRESENTLIST}, {s:old:new:g}, ^表示匹配行开始,$表示匹配行结尾;pif$表示匹配”pif“且字段必须在结尾
    110 ls ${LOAD_DIR}|sed "s:^:${LOAD_DIR}/:"|grep ".pif$" > ${PRESENTLIST}
    111 # 如果存在${LASTLIST}并且不存在${FORMATEDLIST},那么删除${LASTLIST};-a在test中表示and,&&仅当第一个判断为真时才会执行
    112 test -f ${LASTLIST} -a ! -f ${FORMATEDLIST} && rm -f ${LASTLIST}
    113 # 如果存在${FORMATEDLIST}而且不存在${LASTLIST},那么将${FORMATEDLIST}中的内容使用|分割后,取第三个参数赋值到${LASTLIST}
    114 test -f ${FORMATEDLIST} -a ! -f ${LASTLIST} && awk -F"|" '{print $3 > "'${LASTLIST}'"}' ${FORMATEDLIST}
    115 # 如果${FORMATEDLIST}不存在则新建${FORMATEDLIST}
    116 test -f ${FORMATEDLIST} || touch ${FORMATEDLIST}
    117 # 如果${LASTLIST}不存在则新建
    118 test -f ${LASTLIST} || touch ${LASTLIST}
    119 # 将${LASTLIST}中的内容升序排列并重新输出到${LASTLIST}
    120 sort ${LASTLIST} -o ${LASTLIST}
    121 # 取在${PRESENTLIST}出现但未在${LASTLIST}出现的记录到${CFORMATLIST},也就是新增的需要入库的文件
    122 comm -23 ${PRESENTLIST} ${LASTLIST} > ${CFORMATLIST}
    123 # 取在${LASTLIST}中,但不在${PRESENTLIST}中的文件列表,也就是需要删除的文件
    124 comm -23 ${LASTLIST} ${PRESENTLIST} > ${CDELETELIST}
    125 
    126 # 如果${CDELETELIST}存在且可以被读取,那么执行SynLoadFmts这个jar,主要逻辑是从FORMATEDLIST和LASTLIST删除掉CDELETELIST中的内容
    127 test -s ${CDELETELIST} && $JAVA ${JVM_PARAMETER} -jar -Xmx512m ${WORK_DIR}/SynLoadFmts.jar ${FORMATEDLIST} ${LASTLIST} ${CDELETELIST} ${emsentityid}
    128 # 将 ${CFORMATLIST}中的内容解析并输出到${FORMATEDLIST}
    129 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}'"}}' {}
    130 if [ "$ishistorydata" = "0" ];then
    131     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
    132     #取$FILELIST的行数
    133     fileline=`cat $FILELIST|wc -l`
    134     #如果要采集的正常文件不足1000,则进行补采
    135     if [ $fileline -lt $pifcounter ];then
    136         addfilenumber=`expr $pifcounter - $fileline`
    137         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
    138 
    139     fi
    140 else        
    141     #history data
    142     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
    143 
    144     fileline=`cat $FILELIST|wc -l`
    145     if [ $fileline -lt $pifcounter ];then
    146         addfilenumber=`expr $pifcounter - $fileline`
    147         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
    148 
    149     fi
    150 fi
    151 
    152 $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}
    153 rm -f ${WORK_DIR}/$pidfile
    154 rm -f ${WORK_DIR}/.getpifnum.sql
    155 rm -f ${WORK_DIR}/.pifnumresult
    156 rm -f ${WORK_DIR}/.insertpifnum.sql
    157 rm -f ${WORK_DIR}/.selecthistorydate.sql
    158 rm -f ${WORK_DIR}/.ishistorydataresult
    159 rm -f ${WORK_DIR}/.emptyfile
    160 test -f ${PRESENTLIST} && rm -f ${PRESENTLIST}
    161 test -f ${CFORMATLIST} && rm -f ${CFORMATLIST}
    162 test -f ${CDELETELIST} && rm -f ${CDELETELIST}
    163 #echo -e "End:	`date +%Y-%m-%d" "%H:%M:%S`" >> ${PM4H_SPOOL}/${eamid}/${emsentityid}/etc/time
    164 #set +x
    165 exit 0
  • 相关阅读:
    Codeforces Round #263 (Div. 2)
    蓝桥杯 翻硬币
    蓝桥杯 错误的票据
    蓝桥杯 带分数
    蓝桥杯 核桃的数量 求三个数的最小公倍数
    poj 3928 ping pong 树状数组
    lca
    poj 3927 Priest John's Busiest Day
    种类并查集
    高桥和低桥 ( 代代相传刷qq + 无敌二分 )
  • 原文地址:https://www.cnblogs.com/jiyuqi/p/4545586.html
Copyright © 2020-2023  润新知