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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM