zoukankan      html  css  js  c++  java
  • unix 下 shell 遍历指定范围内的日期

     UNIX下遍历日期,date 没有 -d 的参数,所以需要自己处理。

    下面使用时差的方法进行计算,遍历的日期是降序的

    #!/usr/bin/ksh
    . $HOME/.profile
    timelag=16
    cycle=24
    begindate=$1
    enddate=$2
    
    Usage()
    {
        print "Usage: batch_miniestore.sh 开始日期 结束日期"    
        print "       注意:开始时间 >= 结束时间,使用递减方式遍历"
        print "   example:batch_miniestore.sh 20171208 20171203"
    }
    traverse_date()
    {
        if [ ${begindate} -lt ${enddate} ]; then
            print "begindate is:"${begindate}
            print "enddate is:"${enddate}
            Usage
            return 1
        fi
        #当前时间
        today=`date +%Y%m%d`
        print "today is:"${today}
        
        #计算当前时间和指定的起始时间的间隔数
        flag=$((today-begindate))
        print "flag:"${flag}
        
        #根据间隔数确定时差的取值
        if [ flag -eq 1 ]; then
            timelag=16
        elif [ flag -eq 0 ]; then
            timelag=0
        else    
            timelag=$((16+24*(flag-1)))
        fi    
        
        
        curdate=`TZ=aaa${timelag} date +%Y%m%d`
        print "start date is:"$curdate
        
        #遍历时间:从起始时间依次递减进行遍历
        while [ ${curdate} -gt ${enddate} ]
        do
            #根据时差得到起始的时间
            curdate=`TZ=aaa${timelag} date +%Y%m%d`
            print "curdate is:"$curdate
            timelag=$((timelag+cycle))
        done
    }
    traverse_date
  • 相关阅读:
    [ Luogu 3398 ] 仓鼠找sugar
    [ JLOI 2014 ] 松鼠的新家
    AtcoderGrandContest 005 F. Many Easy Problems
    Codeforces 388 D. Fox and Perfect Sets
    Codeforces 1037 H. Security
    「学习笔记」wqs二分/dp凸优化
    「NOI2017」游戏
    「SCOI2014」方伯伯的商场之旅
    「SCOI2015」情报传递
    「SCOI2016」美味
  • 原文地址:https://www.cnblogs.com/etangyushan/p/8092838.html
Copyright © 2011-2022 走看看