需求: 按月執行201904到202010的py腳本。
代碼如下:
#!/bin/bash i=201904 # 定義開始月份 while [ $i -le 202010 ] # 當i小於等於202010時執行循環語句 do dateNo1=$(date +"%Y-%m-%d %H:%M:%S") # 獲取當前時間(年月日時分秒) echo "開始執行($dateNo1): $i" # 打印當前時間(年月日時分秒) python tmp_sn_union_mem_20201125.py $i # 執行python腳本,傳入月份參數。 此處也可做其他操作 dateNo2=$(date +"%Y-%m-%d %H:%M:%S") # 獲取當前時間(年月日時分秒) echo "結束執行($dateNo2): $i" # 打印當前時間(年月日時分秒) i=$[$i + 1] # 當前i值加1 if [ $i -eq 201913 ] # 判斷i值是否等於201913,條件必須寫在 [] 里 then i=202001 # 如果i值等於201913,則讓i值為202001 fi # 容易忘記判斷結束標記 done # 容易忘記循環執行結束標記
注:
fi 容易忘記判斷結束標記 done 容易忘記循環執行結束標記
i=$[$i + 1] 變量加減運算要寫在 $[] 里面。
if [ $i -eq 201913 ] 條件必須寫在 [] 里
附1: date 參數:
附2:數值比較
-eq 相等(equal)
-ne 不等(not equal)
-gt 大於(greater than)
-lt 小於(less than)
-ge 大於等於 (greater than or equal)
-le 小於等於 (less than or equal)