菜鸡一只,今天快速记录下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