日期迴圈,在處理某些按日期存放的資料中很有用。尤其是測試和補錄,刪除,重新處理資料。但是如果遇到跨月等情況,單純用數值迴圈是不行的。
本shell即可用於處理多日資料情況。
代碼如下 |
複製代碼 |
#!/usr/bin/env bash #author: Andy Zhou #Date:2013.8.6 source dateutil.sh begin=20130701 end=20130904 for (( d=$begin; d<=$end; d=`getnextday $d `)); do echo "date:"$d #. myshell.sh $d |
日期工具 dateutil.sh:
代碼如下 |
複製代碼 |
#/usr/bin/env bash #author:Andy Zhou #date:2013.8.2 getnextday() { #date -d "2013-09-10 +1 day " +%Y-%m-%d date -d "$1 +1 day " +%Y%m%d } getyearmonth() { date +%Y%m --date=$1 #shortdate } getday() { date +%d --date=$1 #shortdate } long_date() { date +%Y-%m-%d --date=$1 #shortdate } short_date() { date +%Y%m%d --date=$1 #longdate } long_yesterday() { date --date='1 day ago' +%Y-%m-%d } yesterday() { date --date='1 day ago' +%Y%m%d } long_today() { date +%Y-%m-%d } today() { date +%Y%m%d } now() { date '+%Y-%m-%d %H:%M:%S' } last_month() { date --date='1 month ago' '+%Y%m' } year() { date +%Y } month() { date +%m } sec2date() { date -d "1970-01-01 UTC $1 seconds" "+%Y%m%d" } sec2datetime() { date -d "1970-01-01 UTC $1 seconds" "+%Y%m%d %H:%M:%S" } |