solaris下shell简单脚本
最近写了个关于复制文件,删除文件的脚本,主要功能是通过将一个文件夹下面的文件按时间顺序定时拷入另一个文件夹中,文件形式如附件,文件时间跨越4小时<img alt="">
定时由crontab来执行,每分钟执行一次脚本。
代码如下:
#!/usr/bin/bashmyyear=`date +%Y`mymonth=`date +%m`myday=`date +%d`myhour=`date +%H`mymin=`date +%M`scadafilename=`date '+sz_%Y%m%d_%H%M%S_SCADA.DT~'`pasfilename=`date '+sz_%Y%m%d_%H%M%S_PAS.DT~'`myfilepath="/aems/sz/eformatdata/"targetfilepath="/aems/datastore/sz_data/eformat/"function createfile(){prename="sz_20090523_"suffixname1="_SCADA.DT~"suffixname2="_PAS.DT~"tmp1=`expr ${myhour} % 4`tmp2=`expr ${tmp1} + 7`if(test $tmp2 -ge 10)thenfilehour=$tmp2elsefilehour="0"${tmp2}fi fileprename=${prename}${filehour}${mymin}r1=`ls${myfilepath} | grep ${fileprename} | grep ${suffixname1}`length1=`echo "${r1}" | awk '{print length($0)}'`if (test "${r1}" != "")thenif (test $length1 -eq 28)then`cp ${myfilepath}${r1} ${targetfilepath}${scadafilename}`fifir2=`ls ${myfilepath} | grep ${fileprename} | grep ${suffixname2}`length2=`echo "${r2}" | awk '{print length($0)}'`if (test "${r2}" != "")thenif(test $length2 -eq 26)then`cp ${myfilepath}${r2} ${targetfilepath}${pasfilename}`fifi}createfilefunction cleardata(){if (test $myhour -eq 0)thenif (test $mymin -eq 0)then`find /aems/logs/dataService -name "*.*" -ctime +7 -exec rm -fr {} \``find /aems/logs/deviceService -name "*.*" -exec +7 -exex rm -fr {} \`fififindpreonehourdelscadaname=${targetfilepath}"sz_"${preyear}${premonth}${preday}"_"${prehour}${premin}"00_SCADA.DT~"delpasname=${targetfilepath}"sz_"${preyear}${premonth}${preday}"_"${prehour}${premin}"00_PAS.DT~"if (test -e ${delscadaname}) then`rm ${delscadaname}`fiif (test -e ${delpasname})then`rm ${delpasname}`fi}function findpreonehour(){premin=$myminprehour=`expr ${myhour} - 1`preday=$mydaypremonth=$mymonthpreyear=$myyearif (test $prehour -eq -1)thenprehour=23preday='expr ${myday} - 1`fiif (test $preday -eq 0)thenpremonth=`expr ${mymonth} - 1'fiif (test $premonth -eq 0)thenpremonth=12preyear=`expr ${myyear} - 1`fiif (test $preday -eq 0)thencase $premonth in1|3|5|7|8|10|12) preday=31;;4|6|9|11) preday=30;;2) if (test `expr $preyear % 4` -eq 0)thenpreday=29 elsepreday=28fi;;esacfiphl=`echo ${prehour} | awk '{print length($0)}'`if (test $phl -lt 2)thenprehour="0"$prehourfipdl=`echo ${preday} | awk '{print length($0)}'`if (test $pdl -lt 2)thenpreday="0"$predayfipml=`echo ${premonth} | awk '{print length($0)}'`if (test $pml -lt 2)thenpremonth="0"$premonthfi}cleardata
页:
[1]