shell腳本:輸出昨天的日期

test148-aicboss%cat getlday.sh

#!/bin/bash

#

if [ -n "$1" ]; then

 today=$1

else

 today=`date +"%Y%m%d"`

fi

year=${today:0:4}

month=${today:4:2}

day=${today:6}

M=(0 31 28 31 30 31 30 31 31 30 31 30 31)

if [ $(( ${year}%4 )) -eq 0 ] && ([ $(( ${year}%100 )) -ne 0 ] || [ $(( ${year}%400 )) -eq 0 ] )  ;then

 M[2]=29

fi

if [ $day -eq 1 ]  ;then 

  if[ $month -eq 1 ] ;then

   year=$(( ${year}-1 ))

   month=12

   day=${M[month]}

 else

  month=$(( 10#${month}-1 ))

  day=${M[month]}

  fi

else

 day=$(( 10#${day}-1 ))

fi

echo "昨天的日期是:$year$month$day"


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章