Shell Date Date Loop method: Date format to timestamp calculation, then time stamp back to date format

Source: Internet
Author: User



1, Date object turn timestamp current_day
2, calculates the incremental timestamp, which is the increment of the loop per step one_day
3, loop body calculation, date variable plus increment re-assign value
4, time stamp back to date format output


Current_day=2015-07-17
End_day=2014-01-01
#step 1
Timestamp_current=`date -d "$current_day" +%s`
Timestamp_end=`date -d "$end_day" +%s`
#Step 2
One_day=$((24*60*60))

While [ $timestamp_current -ge $timestamp_end ]
Do
     Echo $timestamp_current
     #Step 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
     #Step 3
     Timestamp_current=$(($timestamp_current-$one_day))
Done 


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.



Shell Date Date Loop method: Date format to timestamp calculation, then time stamp back to date format


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.