shell脚本获取给定年月前一月份及后一月份

抽空制作的一个取前一月份和后一月份脚本

#!/bin/bash
usage()
{
   echo "Usage: $0 201401"
   exit 0
}

if [ x$1 == x ];then
  usage
fi  


year=`echo $1|cut -c 1-4`
month=`echo $1|cut -c 5-6`


next_month()
{
 if [ $2 == 12 ];then
   l_month='01'
   l_year=`expr $1 + 1`
   echo "$l_year$l_month"
 elif [ $2 -lt 9 ];then
   l_year=$1
   l_month=`expr $2 + 1`
   l_month="0$l_month"
   echo "$l_year$l_month"
 else 
   l_year=$1
   l_month=`expr $2 + 1`
   echo "$l_year$l_month"
 fi
}


pre_month()
{
 if [ $2 -eq 1 ];then
   l_month=12
   l_year=`expr $1 - 1`
   echo "$l_year$l_month"
 elif [ $2 -gt 10 ];then
   l_month=`expr $2 - 1`
   l_year=$1
   echo "$l_year$l_month"
 else
   l_month=`expr $2 - 1`
   l_month="0$l_month"
   l_year=$1
   echo "$l_year$l_month"
 fi
}
 
echo "cur_month is $1"


l_next_month=`next_month $year $month`
l_pre_month=`pre_month $year $month`

echo "next month is $l_next_month"
echo "pre month is $l_pre_month"
原文地址:https://www.cnblogs.com/bowshy/p/3538130.html