Saturday 15 March 2014

loops - Create sequence of dates in format yyyymmddHHMM with 12 hour increment in bash -



loops - Create sequence of dates in format yyyymmddHHMM with 12 hour increment in bash -

tl;dr: there nice way of creating sequence of dates 12 hr increment in format yyyymmddhhmm in bash?

consider have python-script accepts time (yyyymmddhhmm) input -t, run instance

python myscript.py -t 201411140000

which starts myscript.pyfor date 2014-11-14 00:00. want run script many dates, origin 2014-01-01 00:00 2014-11-14 00:00 increment of 12 hours, i.e. want produce next 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 201411140000

the closest came (echo verify results without running them):

for mm in {01..10}; dd in {01..31}; hh in 00 12; echo python myscript.py -t 2014$mm$dd$hh\00; done; done; done

it produces required dates jan oct, absurd dates, such 201402310000, programme has handle (i.e. throw/log errors). not huge issue, feels dirty. in end, loop required handle missing dates nov-01 nov-14, which, again, seems dirty me.

how can create dates more nicely - or way above appropriate way of doing so?

you can convert start date unix timestamp, , iterate on 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)); python myscript.py -t $(date +%y%m%d%h%m --date @$ts) done

bash loops command-line

No comments:

Post a Comment