Tl; dr :在bash中以yyyymmddHHMM格式创建一个12小时递增的日期序列是否有很好的方法?
考虑我有一个python脚本,它接受一定的时间(yyyymmddHHMM)作为输入-t,我可以这样运行它,例如
python myscript.py -t 201411140000然后开始myscript.py为2014-11-14 00:00。 现在我想运行许多日期的脚本,从2014-01-01 00:00到2014-11-14 00:00开始,增量为12小时,即我想生成以下所有行:
python myscript.py -t 201401010000 python myscript.py -t 201401011200 python myscript.py -t 201401020000 . . . python myscript.py -t 201411131200 python myscript.py -t 201411140000最接近我的是(回声以验证结果而不运行它们):
for mm in {01..10}; do for dd in {01..31}; do for HH in 00 12; do echo python myscript.py -t 2014$mm$dd$HH\00; done; done; done它产生从1月到10月的所有必需日期,但也有一些荒谬的日期 ,例如201402310000 ,程序然后必须处理(即抛出/记录错误)。 这不是一个大问题,但感觉很脏。 最后,需要另一个循环来处理11月1日至11月14日的缺失日期,这对我来说再次变得肮脏。
我怎样才能更好地创建这些日期 - 或者是否超出了适当的方式?
Tl;dr: Is there a nice way of creating a sequence of dates with 12 hour increment in the format yyyymmddHHMM in the bash?
Consider I have a python-script which accepts a certain time (yyyymmddHHMM) as input -t, i could run it like this for instance
python myscript.py -t 201411140000which the then starts myscript.pyfor the date 2014-11-14 00:00. Now I want to run the script for many dates, beginning from 2014-01-01 00:00 to 2014-11-14 00:00 with an increment of 12 hours, i.e. i want to produce all the following lines:
python myscript.py -t 201401010000 python myscript.py -t 201401011200 python myscript.py -t 201401020000 . . . python myscript.py -t 201411131200 python myscript.py -t 201411140000The closest to what I came is (echo to verify results without running them):
for mm in {01..10}; do for dd in {01..31}; do for HH in 00 12; do echo python myscript.py -t 2014$mm$dd$HH\00; done; done; doneIt produces all required dates from Jan to Oct, but also some absurd dates, such as 201402310000, which the program then has to handle (i.e. throw/log errors). That is not a huge issue, but it feels dirty. In the end, another loop is required to handle the missing dates Nov-01 to Nov-14, which, again, seems dirty to me.
How can I create those dates more nicely - or is the way above the appropriate way of doing so?
最满意答案
您可以将开始日期转换为UNIX时间戳,并以43200秒(12小时)为增量迭代该范围。
for ((ts=$(date +%s --date "2014-11-01 0000"); ts <= $(date +%s --date "2014-11-14 0000"); ts+=12*3600)); do python myscript.py -t $(date +%Y%m%d%H%M --date @$ts) doneYou can convert the start date to a UNIX timestamp, and iterate over the range in 43200-second (12-hour) increments.
for ((ts=$(date +%s --date "2014-11-01 0000"); ts <= $(date +%s --date "2014-11-14 0000"); ts+=12*3600)); do python myscript.py -t $(date +%Y%m%d%H%M --date @$ts) done更多推荐
dates,-t,python,日期,电脑培训,计算机培训,IT培训"/> <meta name="descriptio
发布评论