如何在 Bash 脚本中增加日期 [英] How to increment a date in a Bash script
本文介绍了如何在 Bash 脚本中增加日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 Bash 脚本,它接受格式为 yyyy-mm-dd 的日期参数.
I have a Bash script that takes an argument of a date formatted as yyyy-mm-dd.
我将其转换为秒
startdate="$(date -d"$1" +%s)";
我需要做的是迭代八次,每次将纪元日期增加一天,然后以 mm-dd-yyyy 格式显示.
What I need to do is iterate eight times, each time incrementing the epoch date by one day and then displaying it in the format mm-dd-yyyy.
推荐答案
使用 date
命令能够为现有日期添加天数.
Use the date
command's ability to add days to existing dates.
以下内容:
DATE=2013-05-25
for i in {0..8}
do
NEXT_DATE=$(date +%m-%d-%Y -d "$DATE + $i day")
echo "$NEXT_DATE"
done
产生:
05-25-2013
05-26-2013
05-27-2013
05-28-2013
05-29-2013
05-30-2013
05-31-2013
06-01-2013
06-02-2013
请注意,这在您的情况下效果很好,但其他日期格式(例如 yyyymmdd)可能需要在日期字符串中包含UTC"(例如,date -ud20130515 UTC + 1 天"
).
Note, this works well in your case but other date formats such as yyyymmdd may need to include "UTC" in the date string (e.g., date -ud "20130515 UTC + 1 day"
).
这篇关于如何在 Bash 脚本中增加日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文