首页 > 解决方案 > 如何根据 bash shell 脚本中输入的年份和月份(不是当前月份)获取过去 6 个月的年份和月份?

问题描述

这是一些代码,我试过了。我需要根据输入的年份和月份获取过去 6 个月的数据(年份和月份)。

输入和输出格式:-

*/example program:
  please enter year:2019
  please enter month:05

output:
201904
201903
201902
201901
201812
201811/*

我的脚本:

if [ -z $1 ]
then
   echo Please enter year.
   exit
fi ,

if [ -z $2 ]
then
   echo Please enter month
   exit
fi ,
x=0
while [ "$x" -le 12 ]
do
year= date +'%Y%m' -d "$x month ago"
echo $year
((x++))

标签: bashshelldateconditional-statements

解决方案


try

date --date="$1-$2-01 -6 month" +%B

Demo:

date --date="2019-05-01 -6 month" +'6 month from date was %B!'
6 month from date was November!

推荐阅读