shell中根據日期進行迴圈

2021-08-06 04:07:07 字數 554 閱讀 9807

在寫shell指令碼時,我們經常會有需求,對某乙個日期時間範圍內進行迴圈處理,可以通過設定起止日期,在起止日期範圍內進行迴圈,

```datestart="2016-11-16"

dateend="2017-03-07"

date_s=`date -d "$" +%s`

date_e=`date -d "$" +%s`

while [ "$" -le "$" ];do

statdate=`date -d @$ +"%y-%m-%d"`;

echo $statdate;

# do something

date_s=$((date_s+86400));

done

```date命令 +%s 可以計算指定日期到1970-01-01 00:00:00的秒數(感覺更像是距離1970-01-01 08:00:00的時間)

```date -d "1970-01-01 08:00:00" +%s

#結果是 0 

```86400就是一天24小時的總秒數,如果需要在小時級別進行迴圈,可以每次增加3600

shell 根據變數加減 進行迴圈語句操作

1 while語句 while condition do 迴圈體donecondition 迴圈控制條件 進入迴圈之前,先做一次判 斷 每一次迴圈之後會再次做判斷 條件為 true 則執行 一次迴圈 直到條件測試狀態為 false 終止迴圈 因此 condtion一般應該有迴圈控制變數 而此變數的值...

Shell迴圈獲取日期

大於 gt greater than 小於 lt less than 大於或等於 ge greater than or equal 小於或等於 le less than or equal 不相等 ne not equal bin bash 倒序按天遍歷日期 傳入遍歷的開始時間和結束時間 startd...

根據日期進行分組查詢求和

select from unixtime add time,y m d 日期 sum goods amount 訂單總金額 sum money paid 實際支付總金額 from ecs order info where pay status 2 and add time 1599580800 gr...