linux中用date命令来获取上个月最后一天的日期:
$ date -d"`date +%Y%m01` last day" +%Y%m%d 20161231编写脚本getdate.sh:
#! /bin/sh nowdate=`date +%Y%m01` #本月第一天 startdate=`date -d"$nowdate last month" +%Y%m%d` #上个月第一天 enddate=`date -d"$nowdate last day" +%Y%m%d` #上个月最后一天 #遍历上个月的每一天 while (( $startdate <= $enddate )) do echo $startdate startdate=`date -d "+1 day $startdate" +%Y%m%d` done
运行该脚本的结果:
./getdate.sh 20161201 20161202 20161203 20161204 20161205 20161206 20161207 20161208 20161209 20161210 20161211 20161212 20161213 20161214 20161215 20161216 20161217 20161218 20161219 20161220 20161221 20161222 20161223 20161224 20161225 20161226 20161227 20161228 20161229 20161230 20161231