一例shell脚本,用于清除录音文件,学习下对数据库关联文件的处理办法 一段用于清除录音文件的shell脚本。
脚本很简单,主是要增加了配置读取,每次取出符合条件的号码,再对每个号码对应的录音标识(seq)进行数据库操作及文件操作。
1,配置文件:
log=./log
#源目录 data=/home/wanggy/test_dir #备份目录 topath=/home/wanggy/test_dir1 #mv 0,del 1 flag=0 #间隔时间(s) interval=100 #处理多少天前的数据 days=10 #db dbhost=192.168.1.14 dbuser=wanggy dbpass=12345678 dbbase=gzy_call
2,shell代码:
复制代码代码示例:
#!/bin/bash
(责任编辑:IT)# File Name: mybk_record.sh # site: www.jbxue.com # Created Time: 2013年08月07日 星期三 12时27分30秒 ##------------------------- run_config() { awk -F= '{if ($1 == "'$1'") print$2}' record.cfg } env() { dbhost=`run_config dbhost` dbbase=`run_config dbbase` dbuser=`run_config dbuser` dbpass=`run_config dbpass` flag=`run_config flag` data=`run_config data` topath=`run_config topath` log=`run_config log` interval=`run_config interval` days=`run_config days` echo "获取到的环境变量..." echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" echo "dbhost-->$dbhost" echo "dbbase-->$dbbase" echo "dbusre-->$dbuser" echo "dbpass-->$dbpass" echo "flag-->$flag" echo "data-->$data" echo "topath-->$topath" echo "interval-->$interval" echo "days-->$days" echo "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" sleep 5 } run_main() { while : do run_record_main sleep $interval done } run_record_main() { echo "begin......" env #每次程序运行只执行一次(取大于二条记录录音的主叫号码) echo "#----------------------------------------------------------------------------------------------" echo "[select count(*) from(select SUBSTR(callno,1,INSTR(callno,'#')-1) as callno, count(*)" echo "from record where delflag =0 and begin_time < (now() - INTERVAL $days DAY) and imported = 1" echo "group by SUBSTR(callno ,1,INSTR(callno,'#')-1) having count(*) > 1)]" echo "#----------------------------------------------------------------------------------------------" mysql -u$dbuser -p$dbpass -D$dbbase <<EOF >record select SUBSTR(callno,1,INSTR(callno,'#')-1) as callno, count(*) from record where delflag =0 and begin_time < (now() - INTERVAL $days DAY) and imported = 1 group by SUBSTR(callno ,1,INSTR(callno,'#')-1) having count(*) > 1; EOF sed -i '/callno/d' record line=`wc -l record |awk '{print$1}'` echo "此次共计$line号码需要处理......" sleep 5 #对每个号码对应的录音文件进行操作,保留最新一条 for ((i=1;i<=line;i++));do telno=`awk -v c=$i 'NR==c {print$1}' record` mysql -u$dbuser -p$dbpass -D$dbbase <<EOF >telno select seq,path_relative from record where callno like '$telno%' order by seq desc; EOF sed -i '/seq/d' telno telno_record_line=`wc -l telno |awk '{print$1}'` echo "`date "+%Y-%m-%d %H:%M:%S"` 正在处理$telno....." echo "`date "+%Y-%m-%d %H:%M:%S"` 正在处理$telno....." >>$log/record.log for ((j=2;j<=telno_record_line;j++));do seq=`awk -v c=$j 'NR==c {print$1}' telno` path=`awk -v c=$j 'NR==c {print$2}' telno` mysql -u$dbuser -p$dbpass -D$dbbase <<EOF update record set delflag=1 where seq='$seq'; EOF #判断是否成功 res=`echo $?` if [ $res = 0 ];then case $flag in 1) echo "`date "+%Y-%m-%d %H:%M:%S"` del $seq $data/$path" echo "`date "+%Y-%m-%d %H:%M:%S"` update record set delflag=1 where seq=$seq" echo "`date "+%Y-%m-%d %H:%M:%S"` del $seq $data/$path" >>$log/record.log echo "`date "+%Y-%m-%d %H:%M:%S"` update record set delflag=1 where seq=$seq" >>$log/record.log if [ -f $data/$path ];then rm -rf $data/$path else echo "$data/$path not exist,ignore!!!" fi ;; 0) echo "`date "+%Y-%m-%d %H:%M:%S"` mv $seq $data/$path->$topath/$path" echo "`date "+%Y-%m-%d %H:%M:%S"` update record set delflag=1 where seq=$seq" echo "`date "+%Y-%m-%d %H:%M:%S"` mv $seq $data/$path->$topath/$path" >>$log/record.log echo "`date "+%Y-%m-%d %H:%M:%S"` update record set delflag=1 where seq=$seq" >>$log/record.log m_path=`echo $path |awk '{print$1"/"$2"/"$3"/"$4}'` m_file=`echo $path |awk '{print$5}'` if [ -f $data/$path ];then mkdir -p $topath/$m_path mv $data/$path $topath/$m_path/$m_file else echo "$data/$path not exist,ignore!!!" fi ;; esac else echo "$telno->$seq update err [$res]" echo "$telno->$seq update err [$res]" >$log/record.log fi done done echo "end......" } main() { #env run_main } main exit 0 |