菜雞一隻,今天快速記錄下shell循環連續日期!
因爲畢竟常遇見重跑的任務,然後啓動的spark任務的入口需要傳入重跑的日期,如果日期畢竟少,其實可以這麼寫:
for date in 20191015 20191016 20191017
do
spark-submit \
--master yarn \
--queue xxx \
--name xxx-${date} \
--class xxx \
--deploy-mode client \
--num-executors xx\
--executor-memory xxG \
--driver-memory XXG \
--executor-cores XX \
--driver-java-options "-Dlog4j.configuration=file:///XXX/log4j.properties" \
/XXX/XXX.jar all ${date} ${date} XX
echo ${date} ok...
done
但是如果日期很多的話,而且是連續的,我們就需要寫個循環來處理,否則手寫日期還是比較麻煩的!
如下shell可以獲得連續的日期:
##調用的時候, sh 腳本名稱 20191101 20191203
startdate=`date -d "+0 day $1" +%Y%m%d`
enddate=`date -d "+0 day $2" +%Y%m%d`
while [[ $startdate -le $enddate ]]
do
date=$startdate
##做日期格式的轉換,如果沒有轉換就不需要了
startdateother=`date -d "+0 day $date" +%Y-%m-%d`
dateother=$startdateother
echo "$dateother"
##記得當前循環結束,需要把開始日期+1
startdate=`date -d "+1 day $date" +%Y%m%d`
startdateother=`date -d "+1 day $date" +%Y-%m-%d`
如上的shell腳本是從https://www.cnblogs.com/zhzhang/p/9395843.html(作者:zhzhang)copy來的
所以我的腳本只需要改成這樣
#可以寫死時間,或者從外部傳入
startdate=20191101
enddate=20191123
while [[ $startdate -le $enddate ]]
do
spark-submit \
--master yarn \
--queue xxx \
--name xxx-${startdate} \
--class xxx \
--deploy-mode client \
--num-executors xx\
--executor-memory xxG \
--driver-memory XXG \
--executor-cores XX \
--driver-java-options "-Dlog4j.configuration=file:///XXX/log4j.properties" \
/XXX/XXX.jar all ${startdate} ${startdate} XX
echo ${startdate} ok...
startdate=`date -d "+1 day $startdate" +%Y%m%d`
done