欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Linux:shell完美获取上月,本月,下月年月 上个月 昨天 前天 上一年 有大用 有大大用

Linux:shell完美获取上月,本月,下月年月

版权声明:本文为博主原创文章,出处为 http://blog.csdn.net/silentwolfyh https://blog.csdn.net/silentwolfyh/article/details/52047447
#!/bin/sh
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

#需求:获取上月年月,本月年月,下月年月
#      2016-06       2016-07   2016-08
#
#坑01 
#2016年当month为01的时候,上年月份为2015-12
#2016年当month为12的时候,下年月份为2017-01
#2016年当month为其余月份时候,月份加1或者减一
#
#坑02  
# 1、使用date时候,如果系统时间为2016-05-31
#    `date -d "1 month ago" +%Y-%m` 为2016-05
# 2、使用date时候,如果系统时间为2016-05-31
#    `date -d "1 month " +%Y-%m` 为2016-07
#


year=`date -d "0 month ago" +%Y`

month=`date -d "0 month ago" +%m`

nowdate=`date -d "0 month ago" +%Y-%m` 

one='01'
if [ $month == $one ]
then
echo "当month为01的时候,上年月份为2015-12"
LastYear=$[$year-1]
Lastdate=$LastYear-12
Nextdate=$year-0$[$month+1] 
echo $Lastdate
echo $nowdate
echo $Nextdate
fi

twelve='12'
if [ $month == $twelve ]
then
echo "当month为12的时候,下年月份为2017-01"
NextYear=$[$year+1]
Lastdate=$year-$[$month-1]
Nextdate=$NextYear-01
echo $Lastdate
echo $nowdate
echo $Nextdate
fi


if [ $month -ne $twelve ]
then 
  if [ $month -ne $one ]
  then
      ten=10
      if [ $[$month-1] -lt $ten ]
      then
      monthmin=0$[$month-1]
      else
      monthmin=$[$month-1 ]
      fi

      if [ $[$month+1] -lt $ten ]
      then
      monthadd=0$[$month+1]
      else
      monthadd=$[$month+1 ]
      fi

      echo $year-$monthmin
      echo `date -d "0 month ago" +%Y-%m`
      echo $year-$monthadd
  fi
fi

如果您喜欢我写的博文,读后觉得收获很大,不妨小额赞助我一下,让我有动力继续写出高质量的博文,感谢您的赞赏!微信 

来自  https://blog.csdn.net/silentwolfyh/article/details/52047447

普通分类: