Cycle loops and echoes with bash

I want to iterate all the minutes per month (the goal is to create a CSV file).

But when I try this:

d="2016-09-01 00:00:00"
 while [ "$d" != "2016-09-30 00:00:00" ]; do
 echo $d 
 d=$(date --utc "+%Y-%m-%d %H:%M:00" -d "$d + 1 minute" )
done

Both hours and minutes increase:

2016-09-01 00:00:00
2016-09-01 01:01:00
2016-09-01 02:02:00
2016-09-01 03:03:00
2016-09-01 04:04:00
2016-09-01 05:05:00
2016-09-01 06:06:00
2016-09-01 07:07:00
2016-09-01 08:08:00

What am I doing wrong and how to scroll minutes correctly?

+4
source share
3 answers

I would work with Unix timestamps.

d=$(date --utc +%s -d "2016-09-01 00:00:00")
end=$(date --utc +%s -d "2016-09-30 00:00:00")
while [ "$d" != "$end" ]; do
    date --utc "+%Y-%m-%d %H:%M:00" -d "@$d"
    d=$(( d + 60 ))
done
0
source

You can add the time zone UTC after your variable date string $dto get the correct output:

d="2016-09-01 00:00:00"
for ((i=0; i<=60; i++)); do
   date --utc -d "$d UTC + $i minute"
done

Thu Sep  1 00:00:00 UTC 2016
Thu Sep  1 00:01:00 UTC 2016
Thu Sep  1 00:02:00 UTC 2016
Thu Sep  1 00:03:00 UTC 2016
Thu Sep  1 00:04:00 UTC 2016
Thu Sep  1 00:05:00 UTC 2016
...
...
Thu Sep  1 00:55:00 UTC 2016
Thu Sep  1 00:56:00 UTC 2016
Thu Sep  1 00:57:00 UTC 2016
Thu Sep  1 00:58:00 UTC 2016
Thu Sep  1 00:59:00 UTC 2016
Thu Sep  1 01:00:00 UTC 2016

Pay attention to use UTCafter $d.

0
source

+ , ' , , , , :

$ date  "+%Y-%m-%d %H:%M:00" -d "21:31:00 2016-09-03 + 1 minute  "
2016-09-03 21:32:00

, :

$ TZ=Europe/London date  "+%Y-%m-%d %H:%M:00" -d "2016-09-03 21:32:00 + 1 minute "
2016-09-03 21:33:00
$ TZ=Europe/Brussels date  "+%Y-%m-%d %H:%M:00" -d "2016-09-03 21:32:00 + 1 minute "
2016-09-03 22:33:00
$ TZ=Asia/Singapore date  "+%Y-%m-%d %H:%M:00" -d "2016-09-03 21:32:00 + 1 minute "
2016-09-04 04:33:00
0
source

Source: https://habr.com/ru/post/1657996/


All Articles