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

【shell脚本 实例一】轮询取挂载目录下被更新的文件

时间:2015-12-14 06:46:28      阅读:603      评论:0      收藏:0      [点我收藏+]

标签:

实例一:监测挂载目录下的指定文件状态,一秒轮询,文件被更新即被取到指定目录。

 

 1 #########################################################################
 2 # File Name: getshare.sh
 3 # Author: zoukch
 4 #########################################################################
 5 #!/bin/bash
 6 
 7 if [ $# != 1 ]
 8 then
 9     echo "input wrong!"
10     exit
11 fi
12 
13 if [ -e /mnt/hgfs/share/$1 ]
14 then
15     if [ -e $HOME/workspace/log/getshare.log ]
16     then
17         echo "[`date +%Y%m%d%H%M%S`]getsh.sh started!!!!!!" >> $HOME/workspace/log/getshare.log
18     else
19         touch $HOME/workspace/log/getshare.log
20         echo "[`date +%Y%m%d%H%M%S`]getsh.sh started!!!!!!" >> $HOME/workspace/log/getshare.log
21     fi
22 
23     lastTime=`stat /mnt/hgfs/share/$1 | grep -i Modify | awk -F. {print $1} |24     awk {print $2$3} | awk -F- {print $1$2$3} | awk -F: {print $1$2$3}`
25     echo "[`date +%Y%m%d%H%M%S`]the old modified time of $1 : $lastTime" >> $HOME/workspace/log/getshare.log
26 
27     if [ -e ./$1 ]
28     then
29         cp ./$1 $1.bak
30     fi
31 
32     cp /mnt/hgfs/share/$1 .
33 else
34     echo "[error]the /mnt/hgfs/share/$1 is not exist!"
35     echo "[error]the /mnt/hgfs/share/$1 is not exist!" >> $HOME/workspace/log/getshare.log
36     exit
37 fi
38 
39 while [ -e /mnt/hgfs/share/$1 ]
40 do
41     sleep 1
42     newTime=`stat /mnt/hgfs/share/$1 | grep -i Modify | awk -F. {print $1} |43     awk {print $2$3} | awk -F- {print $1$2$3} | awk -F: {print $1$2$3}`
44 
45     if [ $lastTime != $newTime ]
46     then
47         lastTime=$newTime
48         echo "[`date +%Y%m%d%H%M%S`]the last modified time of $1 : $lastTime" > $HOME/workspace/log/getshare.log
49         echo "MODIFIED FILE at $newTime!" >> $HOME/workspace/log/getshare.log
50         cp ./$1 $1.bak
51         cp /mnt/hgfs/share/$1 .
52         if [ -e ./$1 ]
53         then
54             echo "[`date +%Y%m%d%H%M%S`]get $1 success" >> $HOME/workspace/log/getshare.log
55         fi
56     fi
57 done

 

 

没有阳光、没有蓝天,否则我一定出去走走。

no sunshine,no blue,or I will take a walk.
                   ——zoukch

【shell脚本 实例一】轮询取挂载目录下被更新的文件

标签:

原文地址:http://www.cnblogs.com/zoukch/p/5044031.html

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