0

I want to write a bash script that determines the 10 day period (decade) has ended relative to the start date (in the format YYYY-MM-DD). If the 10 day period is finished script has to output the 10 days period. Im new in bash and has a lot syntax errors with code, help me pls.

#!/bin/bash
# GNU bash, version 4.3.46

CURRENT_DATE=$(date +%Y-%m-%d)
START_DATE=2019-01-01

IS_TODAY_DECADE_CALCULATION_DAY = (CURRENT_DATE - START_DATE) % 10

if [ $IS_TODAY_DECADE_CALCULATION_DAY -eq 0 ]
then
  BEGIN_DATE = $("$CURRENT_DATE - 11 days" +%Y-%m-%d)"
  END_DATE = $("$CURRENT_DATE - 1 day" +%Y-%m-%d)"

  echo "Period is="$BEGIN_DATE":"$END_DATE"
else
  echo "Decade is not finished."
fi
Vadim
  • 753
  • 8
  • 22

2 Answers2

0

You should compare the unix time stamps. If the time stamp "now+10 days" is larger than the start date, the period is ended.

#! /bin/bash

DATE_OLD=$(date "+%F" -d "-11 days")
DATE_NOW=$(date "+%F")

TEST_DATE_NOW=$(date "+%s" -d ${DATE_NOW})
TEST_DATE_OLD=$(date "+%s" -d ${DATE_OLD})

DIVIDER=$(( (TEST_DATE_NOW - TEST_DATE_OLD) / (60*60*24) ))
REMAINING=$(( DIVIDER % 10 ))

echo "Days between ${DATE_OLD} and ${DATE_NOW} is $DIVIDER"

if [ ${DIVIDER} -gt 0 ]; then
        echo "Date ${DATE_OLD} is in the past"
else
        echo "Date ${DATE_OLD} is in the future"
fi

if [ $REMAINING -eq 0 ]; then
        echo "Ten days period ended"
else
        echo "Still in ten day period"
fi

exit 0;
Bayou
  • 3,293
  • 1
  • 9
  • 22
  • Grand thanks, Could you tell me how to make the script check that dividing the remainder of the number of days by 10 is zero. And output yesterday in the format YYYY-MM-DD. – Vadim Oct 04 '19 at 09:48
  • @Vadim is this what you're looking for? – Bayou Oct 04 '19 at 11:05
0

The question implies that the code should identify each 10 day period starting on a specific START_DATE. Bash does not have date math - it can not calculate difference between dates (as expected by '(CURRENT_DATE - START_DATE)'). Two options

  1. Convert date to seconds since Unix Epoch, and do the math on those values, OR
  2. Use date utilities package, OR
  3. using Python, awk, perl

Implementing #1 is simple. Notice few changes to assignments - in particular no spaces are allowed in assignments variable=expression, or let variable=expression

#! /bin/bash

CURRENT_DATE=$(date +%Y-%m-%d)
START_DATE=2019-01-01
# Instead of IS_TODAY_DECADE_CALCULATION_DAY = (CURRENT_DATE - START_DATE) % 10

SEC_IN_DAY=$((60*60*24))
let D1=$(date '+%s' -d "$CURRENT_DATE Z")/SEC_IN_DAY
let D2=$(date '+%s' -d "$START_DATE Z")/SEC_IN_DAY
let IS_TODAY_DECADE_CALCULATION_DAY=(CURRENT_DATE-START_DATE)%10

# Rest of script here
if [ $IS_TODAY_DECADE_CALCULATION_DAY -eq 0 ]
then
  BEGIN_DATE=$(date -d "$CURRENT_DATE - 11 days" +%Y-%m-%d)
  END_DATE=$(date -d "$CURRENT_DATE - 1 day" +%Y-%m-%d)

  echo "Period is=$BEGIN_DATE:$END_DATE"
else
  echo "Decade is not finished."
fi
dash-o
  • 13,723
  • 1
  • 10
  • 37
  • Grand thanks, its work. Could you change the 2019-01-01 to 2019-1-1 format. – Vadim Oct 04 '19 at 11:09
  • Happy to help. If this answer or any other one solved your issue, please mark it as accepted – dash-o Oct 04 '19 at 11:21
  • @Vadim the '2019-1-1' format is not standard. Linux date will support it using the '%-d' format. You can replace +'%Y-%m%d' in "BEGIN_DATE=" and 'END_DATE=' with '%Y-%-m-%-d". See date man page – dash-o Oct 04 '19 at 12:47