How to loop through dates using Bash?

后端 未结 8 748
一整个雨季
一整个雨季 2020-12-02 06:22

I have such bash script:

array=( \'2015-01-01\', \'2015-01-02\' )

for i in \"${array[@]}\"
do
    python /home/user/executeJobs.py {i} &> /home/user/         


        
8条回答
  •  旧时难觅i
    2020-12-02 06:30

    If you're stuck with busybox date, I've found working with timestamps to be the most reliable approach:

    STARTDATE="2019-12-30"
    ENDDATE="2020-01-04"
    
    start=$(date -d $STARTDATE +%s)
    end=$(date -d $ENDDATE +%s)
    
    d="$start"
    while [[ $d -le $end ]]
    do
        date -d @$d +%Y-%m-%d
    
        d=$(( $d + 86400 ))
    done
    

    This will output:

    2019-12-30
    2019-12-31
    2020-01-01
    2020-01-02
    2020-01-03
    2020-01-04
    

    Unix timestamp don't include leap seconds, so 1 day equals always exactly 86400 seconds.

提交回复
热议问题