一、創建測試文件test.sh
#!/bin/sh. /etc/profile# 參數:# args[0] ,數據日期,日期格式yyyy-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
二、無參運行
## 不帶參數,從當前時間開始算$ ./test.sh2016-10-282016-09-29
三、傳參運行
$ ./test.sh 2015-10-292015-10-292015-09-29
總結
以上就是利用shell獲取指定日期前N天日期的全部內容,希望本文的內容對大家的學習或者工作能有所幫助,如果有疑問大家可以留言交流。
新聞熱點
疑難解答