实例一:监测挂载目录下的指定文件状态,一秒轮询,文件被更新即被取到指定目录。
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