如何增加在bash脚本的日期 [英] How to increment a date in a bash script

查看:95
本文介绍了如何增加在bash脚本的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个bash脚本,需要一个日期的参数(YYYY-MM-DD)

I have a bash script that takes an argument of a date (yyyy-mm-dd)

我把它转换成秒

startdate="$(date -d"$1" +%s)";

我需要做的是迭代8次,每次1天递增时代日期,然后在格式MM-DD-YYYY显示它

What I need to do is iterate 8 times, each time incrementing the epoch date by 1 day and then displaying it in the format mm-dd-yyyy

推荐答案

使用日期命令对天添加到现有的日期的能力。

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的日期字符串(如日期-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 (eg. date -ud "20130515 UTC + 1 day")

这篇关于如何增加在bash脚本的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆