How to loop through dates using Bash?

后端 未结 8 733
一整个雨季
一整个雨季 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条回答
  •  北海茫月
    2020-12-02 06:39

    Using GNU date:

    d=2015-01-01
    while [ "$d" != 2015-02-20 ]; do 
      echo $d
      d=$(date -I -d "$d + 1 day")
    done
    

    Note that because this uses string comparison, it requires full ISO 8601 notation of the edge dates (do not remove leading zeros). To check for valid input data and coerce it to a valid form if possible, you can use date as well:

    # slightly malformed input data
    input_start=2015-1-1
    input_end=2015-2-23
    
    # After this, startdate and enddate will be valid ISO 8601 dates,
    # or the script will have aborted when it encountered unparseable data
    # such as input_end=abcd
    startdate=$(date -I -d "$input_start") || exit -1
    enddate=$(date -I -d "$input_end")     || exit -1
    
    d="$startdate"
    while [ "$d" != "$enddate" ]; do 
      echo $d
      d=$(date -I -d "$d + 1 day")
    done
    

    One final addition: To check that $startdate is before $enddate, if you only expect dates between the years 1000 and 9999, you can simply use string comparison like this:

    while [[ "$d" < "$enddate" ]]; do
    

    To be on the very safe side beyond the year 10000, when lexicographical comparison breaks down, use

    while [ "$(date -d "$d" +%Y%m%d)" -lt "$(date -d "$enddate" +%Y%m%d)" ]; do
    

    The expression $(date -d "$d" +%Y%m%d) converts $d to a numerical form, i.e., 2015-02-23 becomes 20150223, and the idea is that dates in this form can be compared numerically.

提交回复
热议问题