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

 

posted on 2017-12-23 10:44  欢跳的心  阅读(2548)  评论(0编辑  收藏  举报