码迷,mamicode.com
首页 > 系统相关 > 详细

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

时间:2015-05-05 06:41:36      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:linux   脚本   shell   

     今天接到有关部门反映,存储的空间不够了。需要删除一些视频文件来腾出空间。由于刚接手工作没多久,上任写的是python脚本。无奈,个人python水平还是入门。所以只能另写个shell脚本来完成工作了。

 

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

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

      思路:

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

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

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

好了,开整:

  现状:

[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 |
+-----------+---------------------------------------------------------------------+------------+

      脚本思路:

      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 |
+------------+

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

 

 

 

本文出自 “梁恩宇-9527” 博客,请务必保留此出处http://liangey.blog.51cto.com/9097868/1641878

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

标签:linux   脚本   shell   

原文地址:http://liangey.blog.51cto.com/9097868/1641878

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!