shell的date日期循环方法:日期格式转时间戳计算,再将时间戳转回日期格式

时间:2024-01-13 10:33:20

1,日期对象转时间戳current_day

2,计算增量的时间戳,即循环每步的增量one_day

3,循环体计算,日期变量加增量后重新赋值自己

4,时间戳转回日期格式后输出

current_day=2015-07-17
end_day=2014-01-01
#步骤1
timestamp_current=`date -d "$current_day" +%s`
timestamp_end=`date -d "$end_day" +%s`
#步骤2
one_day=$((24*60*60)) while [ $timestamp_current -ge $timestamp_end ]
do
echo $timestamp_current
#步骤4
date -d @$timestamp_current +%Y-%m-%d
sh /opt/uv_report/hive_2_hdfs_ad_day.sh /user/wizad/uv_report/uv_day/ $timestamp_current
exitCode=$?
if [ $exitCode -ne 0 ];then
echo "[ERROR] uv_day for $timestamp_current"
exit $exitCode
fi
sh /usr/local/datahub/DataHub.sh /opt/uv_report/uv_day_2_mysql.xml -p /opt/uv_report/uv_day_2_mysql_params -d job.business.date=2015-07-30
if [ $exitCode -ne 0 ];then
echo "[ERROR] insert into mysql table for $timestamp_current"
exit $exitCode
fi
#步骤3
timestamp_current=$(($timestamp_current-$one_day))
done