根据字段状态删除指定目录文件的shell脚本

           声明:如下操做均为在虚拟机上进行的,毕竟生产环境是不可以乱来的,因此测试OK以后呢,再到线上执行脚本方可。mysql

      要求:删除/data/video/sports/shi/下面的视频sql

      思路:bash

     1.首先有关部门已经将须要删除的目录,字段 statusCode改成0,默认为1ide

     2.根据statusCode的状态查询出要删除的路径测试

     3.删除视频文件完毕后,须要将statusCode的状态改成1  spa

好了,开整:视频

  现状:ip

[root@localhost shi]# ll
total 0
-rw-r--r-- 1 root root 0 May  4 09:38 hc2012051620.mp4
-rw-r--r-- 1 root root 0 May  4 09:38 hc2012051621.mp4
-rw-r--r-- 1 root root 0 May  4 09:38 hc2012051622.mp4
-rw-r--r-- 1 root root 0 May  4 09:38 hc2012051623.mp4

 

[root@localhost shi]# mysql -uroot -proot -e "use sne;select ContentID,PlayAddress,statusCode from del_video_file;"
+-----------+---------------------------------------------------------------------+------------+
| ContentID | PlayAddress                                       | statusCode |
+-----------+---------------------------------------------------------------------+------------
|    128704 | http://xxx:17533/gdtv/sports/shi/hc2012051620.mp4 |          0 |
|    128705 | http://xxx:17533/gdtv/sports/shi/hc2012051621.mp4 |          0 |
|    128706 | http://xxx:17553/gdtv/sports/shi/hc2012051622.mp4 |          0 |
|    128707 | http://xxx:17553/gdtv/sports/shi/hc2012051623.mp4 |          0 |
+-----------+---------------------------------------------------------------------+------------+

      脚本思路:rem

      1.登陆mysql查询statusCode为0的记录,而且用awk取到所须要的路径虚拟机

#!/bin/bash
#author:ley
#声明路径的变量,由于sql中的路径并非绝对路径
datadir='/data/video'

result=`mysql -uroot -proot -e 
"use sne;
select ContentID,PlayAddress from del_video_file where statusCode=0;"
|awk 'BEGIN{FS="17553/"} {print $2}'`

      2.用for循环依次删除视频文件

for i in $result
do
   rm -rf  $datadir/$i
   echo "remove the file is ok"
done

      3.根据statusCode=0查询出对应的ContentID

ContentID=
`mysql -uroot -proot -e "use sne;
select ContentID,PlayAddress from del_video_file where statusCode=0;"|awk '{print $1}'`

      4.再用for循环依次更新statusCode=1

for id in $ContentID
 do 
update=`mysql -uroot -proot -e 
"use sne;
update del_video_file set statusCode=1 where ContentID=$id;"`

if [ $? == 0 ]
 then
   echo "update is ok"
 else
   echo "update is error"
fi

done

       5.查看statusCode的状态已经为1了

[root@localhost script]# mysql -uroot -proot -e 
"use sne;select statusCode from del_video_file;"
+------------+
| statusCode |
+------------+
|          1 |
|          1 |
|          1 |
|          1 |
+------------+

     好了,脚本大概就是这样子了。一些细节问题,从此继续完善吧。