我試圖回圈過去 25 個月,但我的命令是跳過 2 月并復制 3 月
命令
for i in {00..25}; do for ym in $(date -d "-$i month" %Y-%m); do echo ${ym}; done; done
輸出
2022-04
2022-03
2022-03 --this needs to be Feb
2022-01
2021-12
2021-11
2021-10
2021-09
2021-08
2021-07
2021-06
2021-05
2021-04
2021-03
2021-03 --this needs to be Feb
2021-01
2020-12
2020-11
2020-10
2020-09
2020-08
2020-07
2020-06
2020-05
2020-04
2020-03
如何修改它以使其按預期作業?
uj5u.com熱心網友回復:
按照我的評論:
$ date -ud '2022-04-29 -2 month'
Tue Mar 1 00:00:00 UTC 2022
需要“固定”這一天,使其對所有月份都有效(不管“當前日期”是什么)
this_month=$(date -u ' %Y-%m-01') # the first day of this month
for i in {0..25}; do date -ud "$this_month -$i month" ' %Y-%m'; done
輸出
2022-04
2022-03
2022-02 <<<
2022-01
2021-12
2021-11
2021-10
2021-09
2021-08
2021-07
2021-06
2021-05
2021-04
2021-03
2021-02 <<<
2021-01
2020-12
2020-11
2020-10
2020-09
2020-08
2020-07
2020-06
2020-05
2020-04
2020-03
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/467881.html
上一篇:正確宣告變數時出現“未找到”錯誤
下一篇:bash中變數的簡化
