shell:bash根據指定日期 擷取前一天 和 後一天的日期

來源:互聯網
上載者:User

標籤:date

####################################################################### 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

shell:bash根據指定日期 擷取前一天 和 後一天的日期

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.