ループが間違った日付を生成している間

ループが間違った日付を生成している間

次のループがありますが、停止せずに間違った日付を生成します。

#!/bin/bash
i=0
thedate="2018-03-28"
enddate="2018-04-02"
while [ "$thedate" != "$enddate" ]; do
    thedate=$( date -d "$thedate + $i days" +%F )

    new_date=$( date -d "$thedate " +%Y%m%d )
    printf 'The date is "%s"\n' "$new_date"
    i=$(( i + 1 ))
done

次のような結果を期待しています。

The date is "20180328"
The date is "20180329"
The date is "20180330"
The date is "20180331"
The date is "20180401"
The date is "20180402"

この目標をどのように達成できますか?

ベストアンサー1

iカウンターはまったく必要ありません。
各繰り返しごとに現在の日付を1ずつ増やすだけです。

#!/bin/bash
thedate="2018-03-28"
enddate="2018-04-02"
while [ "$thedate" != "$enddate" ]; do
    thedate=$( date -d "$thedate + 1 days" +%F )

    new_date=$( date -d "$thedate " +%Y%m%d )
    printf 'The date is "%s"\n' "$new_date"
done

おすすめ記事