shell指令碼案例--擷取當前日期的前一天日期

來源:互聯網
上載者:User

標籤:

記錄一個shell指令碼案例,案例中包括shell的一些文法(函數定義、運算式運算、if、case...)

#!/bin/sh

#擷取目前時間
RUN_TIME=`date +%H%M%S`
#取當前日期前一天
get_ydate()
{
        #設定當前年月日
        TODAY=`date +%Y%m%d`
        month=`date +%m`
        day=`date +%d`
        year=`date +%Y`
        #字元轉換稱數字
        month=`expr $month + 0`
        #取前一天
        day=`expr $day - 1`

        #如果前一天為0,進行如下處理   
        if [ $day -eq 0 ]; then
          #取前一月月份.
          month=`expr $month - 1`
          #如果前一月為0,則取前一年,月份取第12月       
          if [ $month -eq 0 ];then
            month=12
            day=31
            year=`expr$year - 1`
          #如果前一月份不為零,則根據月份來判斷該天是該月的第幾天          
          else
            case $month in
             1|3|5|7|8|10|12) day=31;;
             4|6|9|11) day=30;;
             2)
               if [ `expr $year % 4` -eq 0 ]; then
                 if [ `expr $year % 400` -eq 0 ]; then
                   day=29
                 elif [ `expr $year % 100` -eq 0 ]; then
                   day=28
                 else
                   day=29
                 fi
               else
                 day=28
               fi
             ;;
             esac
          fi
        fi
        #按照YYYYMMDD格式列印年日期    
        acct_date=`printf "%04d%02d%02d""$year" "$month" "$day"`
        echo $RUN_TIME
        echo $acct_date
}
get_ydate

shell指令碼案例--擷取當前日期的前一天日期

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.