shell:bash根据指定日期 获取前一天 和 后一天的日期

####################################################################### Copyright C 2014 ## All rights reserved ## Name: yestoday.sh# Author: Canux [email protected] ## Version: V1.0 ## Time: Wed 12 Nov 2014 04:37:37 PM CST# Description: ########################################################################!/bin/bashif [ "$1" = "" ]then DATE_NO=`date +%Y%m%d` CURDAY=`date +%d` CURMONTH=`date +%m` CURYEAR=`date +%Y`else DATE_NO=$1 CURYEAR=`echo $1 | awk ‘{print substr($0,1,4)}‘` CURMONTH=`echo $1 | awk ‘{print substr($0,5,2)}‘` CURDAY=`echo $1 | awk ‘{print substr($0,7,2)}‘`fiLASTYEAR=`expr $CURYEAR - 1`if [[ $((10#$CURMONTH)) -eq 1 ]]then LASTMONTH=12else LASTMONTH=`expr $CURMONTH - 1`fiif [[ $LASTMONTH -ge 1 ]] && [[ $LASTMONTH -le 9 ]]then LASTMONTH=0$LASTMONTHelse LASTMONTH=$LASTMONTHfiNUM1=`cal $LASTMONTH $CURYEAR | awk ‘NR==6{print $NF}‘`NUM2=`cal $LASTMONTH $CURYEAR | awk ‘NR==7{print $NF}‘`NUM3=`cal $LASTMONTH $CURYEAR | awk ‘NR==8{print $NF}‘`if [[ $NUM3 -ne 0 ]]then LMLASTDAY=$NUM3elif [[ $NUM3 -eq 0 ]] && [[ $NUM2 -ne 0 ]]then LMLASTDAY=$NUM2else LMLASTDAY=$NUM1fiif [[ $((10#$CURDAY)) -eq 1 ]] then if [[ $((10#$CURMONTH)) -eq 1 ]] then LASTDATE=$LASTYEAR$LASTMONTH$LMLASTDAY else LASTDATE=$CURYEAR$LASTMONTH$LMLASTDAY fielse LASTDATE=`expr $DATE_NO - 1`fiecho $LASTDATE####################################################################### Copyright C 2014 ## All rights reserved ## Name: tomorrow.sh# Author: Canux [email protected] ## Version: V1.0 ## Time: Wed 12 Nov 2014 04:52:14 PM CST# Description: ########################################################################!/bin/bashif [ "$5" = "" ]then CURDATE=`date +%Y%m%d` CURDAY=`date +%d` CURMONTH=`date +%m` CURYEAR=`date +%Y` CURHOUR=`date +%H` CURMIN=`date +%M`else CURDATE=$8 CURYEAR=`echo $2 | awk ‘{print substr($0,1,4)}‘` CURMONTH=`echo $2 | awk ‘{print substr($0,ENDHOUR,2)}‘` CURDAY=`echo $2 | awk ‘{print substr($0,7,2)}‘`fiNEXTYEAR=`expr $CURYEAR + 1`if [[ $((10#$CURMONTH)) -eq 12 ]]then NEXTMONTH=1else NEXTMONTH=`expr $CURMONTH + 1`fiif [[ $((10#$NEXTMONTH)) -ge 1 ]] && [[ $((10#$NEXTMONTH)) -le 9 ]]then NEXTMONTH=0$((10#$NEXTMONTH))else NEXTMONTH=$NEXTMONTHfi#the last day of curent monthNUM1=`cal $CURMONTH $CURYEAR | awk ‘NR==6{print $NF}‘`NUM2=`cal $CURMONTH $CURYEAR | awk ‘NR==7{print $NF}‘`NUM3=`cal $CURMONTH $CURYEAR | awk ‘NR==8{print $NF}‘`if [[ $NUM3 -ne 0 ]]then LASTDAY=$NUM3elif [[ $NUM3 -eq 0 ]] && [[ $NUM2 -ne 0 ]]then LASTDAY=$NUM2else LASTDAY=$NUM1fiif [[ $((10#$CURMONTH)) -eq 12 ]] && [[ $((10#$CURDAY)) -eq 31 ]]then NEXTDATE=${NEXTYEAR}0101elif [[ $((10#$CURDAY)) -eq $((10#$LASTDAY)) ]]then NEXTDATE=${CURYEAR}${NEXTMONTH}01else NEXTDATE=`expr $CURDATE + 1`fiecho $NEXTDATE

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。