一、創(chuàng)建測試文件test.sh
#!/bin/sh. /etc/profile# 參數(shù):# args[0] ,數(shù)據(jù)日期,日期格式y(tǒng)yyy-MM-dd# 取30天以前的日期function get_date_30daysbefore(){ sec=`date -d $1 +%s` sec_30daysbefore=$((sec - 86400*30)) days_before=`date -d @$sec_30daysbefore +%F` echo $days_before}if [ $# == 1 ]; then today=$1 dates_30=`get_date_30daysbefore $1`else today=`date -d -1days '+%Y-%m-%d'` dates_30=`date -d -30days '+%Y-%m-%d'`fiecho $todayecho $dates_30二、無參運(yùn)行
## 不帶參數(shù),從當(dāng)前時間開始算$ ./test.sh2016-10-282016-09-29
三、傳參運(yùn)行
$ ./test.sh 2015-10-292015-10-292015-09-29
總結(jié)
以上就是利用shell獲取指定日期前N天日期的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能有所幫助,如果有疑問大家可以留言交流。
新聞熱點
疑難解答
圖片精選