shell關於日期的加減


$cat sh.sh 
#!/bin/bash

if [ $# == 0 ] ; then
    ydate=`date -d -1days "+%Y%m%d"`
    ydateother=`date -d -1days "+%Y-%m-%d"`
    tdate=`date -d -0days "+%Y%m%d"`
    tdateother=`date -d -0days "+%Y-%m-%d"`
elif [ $# == 1 ] ; then 
    ydate=$1
    ydateother=`date -d ${ydate} +"%Y-%m-%d"`
    tdate=`date -d "${ydate} 1days" +"%Y%m%d"`
    tdateother=`date -d ${tdate} +"%Y-%m-%d"`
elif [ $# == 2 ] ; then 
    ydate=$1
    ydateother=`date -d ${ydate} +"%Y-%m-%d"`
    tdate=$2
    tdateother=`date -d ${tdate} +"%Y-%m-%d"`
else
    echo "please do not input if calcute yedterday."
    echo "please input like if calcute 20170510 one day: 20170510"
    echo "please input like if calcute 20170510 and 20170512 two days : 20170510 20170511"
    exit -1
fi 
echo ${ydate}'--'${ydateother}'--'${tdate}'---'${tdateother}

 

傳入字符串,修改為日期;

根據傳入的字符串識別的日期,進行加減

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM