shell 对日期的循环(天和小时)

1.按天倒序遍历天


#!/bin/bash
firstDay=`date -d "-9 day" +%Y%m%d`
endDay=`date -d " -7 day" +%Y%m%d`
#遍历下个月的每一天
source /opt/huawei/Bigdata/hiveClient/bigdata_env
while (( $firstDay <= $endDay ))
do
beeline -e "drop table dw.dw_doc_department_$firstDay "

firstDay=`date -d "+1 day $firstDay" +%Y%m%d`

done

 

2.按小时遍历日期


#!/bin/bash
#以小时循环
#sh x.sh 2017010101 2017010301


stime=`date -d "-72 hours" +%Y%m%d%H`
etime=`date -d "-48 hours" +%Y%m%d%H`
source /opt/huawei/Bigdata/hiveClient/bigdata_env
while :
do
echo $stime
beeline -e "drop table ods.ods_order_info_dd_${stime}"
beeline -e "drop table ods.ods_user_info_dd_${stime} "
stime=$(date -d "${stime:0:8} ${stime:8:2} 1hour" +%Y%m%d%H)
if [[ $stime -gt $etime ]]
then
break
fi
done



RUSH B
原文地址:https://www.cnblogs.com/tangsonghuai/p/10876617.html