Pacific-Design.com

    
Home Index

1. Bash Shell

2. Period Year Month

Bash Shell / Period Year Month /

Calculate Monthly Period - Display Year and Month

#!/bin/bash
#-----------------------------------------------------#
PAR1=$1
LEN2=$2

DATE=$PAR1'01'
echo ${DATE:0:6}

for i in `seq 1 ${LEN2}`;
do
   NEXT_MONTH=$(date +%Y%m%d -d "$DATE - $i month")
   echo ${NEXT_MONTH:0:6}
done

#-----------------------------------------------------#

Output

$ ./calculate-period.sh 201803 12
 201803
 201802
 201801
 201712
 201711
 201710
 201709
 201708
 201707
 201706
 201705
 201704
 201703