Chinaunix首页 | 论坛 | 博客
  • 博客访问: 81659
  • 博文数量: 10
  • 博客积分: 202
  • 博客等级: 入伍新兵
  • 技术积分: 221
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-12 20:03
个人简介

一枚老菜鸟 吐槽基地:http://xuxiake1981.blog.hexun.com/

文章分类
文章存档

2020年(1)

2017年(1)

2013年(3)

2012年(5)

分类: 系统运维

2013-01-05 19:39:45


点击(此处)折叠或打开

  1. #!/bin/ksh
  2. ########################################################
  3. #Date: 2012-12-19
  4. #name: swiftftp.sh
  5. #decription: 无限循环,每1分钟执行一次,查找当前目录中修改时间超过1分钟但未超过
  6. # 2分钟的文件,ftp到前置上
  7. ########################################################


  8. datestr=`date "+%Y%m%d"`
  9. _logname=dynamicftp${datestr}.log
  10. chmod +x *
  11. if [[ ! -f ${_logname} ]];then
  12.     touch ${_logname};
  13. fi

  14. #该函数打印参数1的时间戳
  15. # perl usage: perl -e "$a=1+4;exit($a);"
  16. cal_timestamp()
  17. {
  18.    perl -e '
  19.     use Time::Local;

  20. # get current time
  21. # 参数表示,要求返还minu_num个分钟之前的时间戳yyyymmddhhmm.ss
  22.     $minu_num="$ARGV[0]";
  23.           localtime(time);

  24.        $time1= time -60*$minu_num;
  25.        ($sec,
  26.           $min,
  27.           $hr,
  28.           $mday,
  29.           $mon,
  30.           $yr,
  31.           $wday,
  32.           $yday,
  33.           $isdst)=localtime($time1);
  34.   
  35.         $yr=$yr+1900;
  36.         $mon=$mon+1;
  37.         $beforetime=sprintf("%d%02d%02d%02d%02d.%02d",$yr,$mon,$mday,$hr,$min,$sec);
  38.         print "$beforetime"
  39.         ' "$1" #把函数的第一个参数,用这种形式传递给perl
  40. }


  41. #最外层循环
  42. while [[ 1 -eq 1 ]] ; do
  43. # example use of cal_timestamp:
  44.     oneminubef=$( cal_timestamp "1")
  45.     echo "1 minutes before:$oneminubef" >> ${_logname}
  46.     twominubef=$( cal_timestamp "2")
  47.     echo "2 minutes before:$twominubef" >> ${_logname}
  48.     
  49.     # create two older files
  50.     touch -t $oneminubef oneminubef.tmp
  51.     touch -t $twominubef twominubef.tmp
  52.     
  53.     #find the file list which is younger than 2 minutes but older than 1 minutes
  54.     #find usage: find -newer file1 ! file2
  55.     
  56.     touch tempfile01
  57.     chmod 777 tempfile01
  58.     find . -newer twominubef.tmp ! -newer oneminubef.tmp | grep -v '^./oneminubef.tmp' |grep -v "^./${_logname}" |grep -v "{$0}" > tempfile01
  59.     
  60.     #read the file and strcat the files
  61.     a=0
  62.     filenames=
  63.     while read line
  64.     do
  65.       line=`echo $line | sed -e 's/^ *//' -e 's/ *$//'` #去除空格
  66.       length=`echo ${line} | awk '{print length($1);}'` #计算字符数
  67.       if [[ ${length} -gt 0 ]]; then #防止空行
  68.         newline=${line#*/} #去除文件名前面的./
  69.         filenames=$filenames" "$newline
  70.         a=`expr $a + 1`
  71.       fi
  72.     done < tempfile01
  73.     rm -rf tempfile01 oneminubef.tmp twominubef.tmp
  74.     
  75.     echo "files :$filenames" >> ${_logname}
  76.     
  77.   
  78.     _username=test
  79.     _passwd=test
  80.     
  81.     if [[ $a -gt 0 ]]; then
  82.         ftp -in 192.168.93.77 << !
  83.                 user ${_username} ${_passwd}
  84.                 bi
  85.              mput $filenames
  86.                 bye
  87. !
  88. fi
  89.         
  90. if [[ $? -eq 0 && a -gt 0 ]]; then
  91.                     echo `date`" successfully put $a files on server" >> ${_logname}
  92. elif [[ a -gt 0 ]]; then
  93.         echo `date`" put files error!" >> ${_logname}
  94.     else
  95.         echo `date`" no files need to be put!" >> ${_logname}
  96.     fi
  97.     sleep 60
  98. done

阅读(3503) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~