• Uncategorized

About linux : How-to-Sum-Date-with-Bash

Question Detail

I need to take the Epoch number followed by the date and time in 15 minute intervals. The result should be something like this:

1376708400|2013-08-17 00:00:00
1376709300|2013-08-17 00:15:00
1376710200|2013-08-17 00:30:00
1376711100|2013-08-17 00:45:00
1376712000|2013-08-17 01:00:00
1376712900|2013-08-17 01:15:00
1376713800|2013-08-17 01:30:00
1376714700|2013-08-17 01:45:00
1376715600|2013-08-17 02:00:00
1376716500|2013-08-17 02:15:00
1376717400|2013-08-17 02:30:00

… And so on… In the total I should have 96 lines.

My biggest doubt right now is how can I increment (or sum) the minutes by 15 minutes, I have tried this (Forgive my mess, I’m still learning):

i=96
_incrementatime='+15 minutos'
count=1

_Date=$(`date "2013-08-29 00:00:00"`)
_dataEmSeg = `date -d "2013-08-29 00:00:00" +%s`

while  test $i -ne 0
do
    _VarData=$(`date --date="$_incrementatime*count"`)

    _exprt=$(expr `$_Date+$_VarData`)
    echo "$_dataEmSeg  e  $_exprt    "

    i=$((i-1))
    count=$((count+1))
done

If anyone can give me some directions to resolve this, I’d be very grateful.

Question Answer

You can base on this:

#!/bin/bash

DATE='2013-08-29 00:00:00'
TIMESTAMP=$(date -d "$DATE" '+%s')
INCREMENT=$(( 60 * 15 ))
STEPS=5

echo "$TIMESTAMP|$DATE"

for (( I = 1; I <= STEPS; ++I )); do
    (( TIMESTAMP += INCREMENT ))
    DATE=$(date -d "@$TIMESTAMP" '+%F %T')
    echo "$TIMESTAMP|$DATE"
done

Output:

1377705600|2013-08-29 00:00:00
1377706500|2013-08-29 00:15:00
1377707400|2013-08-29 00:30:00
1377708300|2013-08-29 00:45:00
1377709200|2013-08-29 01:00:00
1377710100|2013-08-29 01:15:00

Your increment line:

VarData=$(`date --date="$_incrementatime*count"`)

should be:

VarData=$(`date --date="$_incrementatime*$count"`)

if you want the count variable displayed.

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.