In linux, shell processes daily data cyclically.

Source: Internet
Author: User

This article introduces how to use shell to process daily data instances in linux. You can refer to this article for operations based on the directory date, but it is not applicable to cross-month operations.

Date loops are useful in processing data stored by date. Especially test and record, delete, and reprocess data. However, in the case of cross-month or other situations, it is impossible to simply use a numerical loop.
This shell can be used to process data from multiple days.

The Code is as follows: Copy code

#! /Usr/bin/env bash
# Author: Andy Zhou
# Date: 2013.8.6

Source dateutil. sh
Begin in = 20130701
End = 20130904

For (d = $ begin; d <= $ end; d = 'getnextday $ D'); do
Echo "date:" $ d
#. Myshell. sh $ d

Date tool dateutil. sh:

The Code is as follows: Copy code

#/Usr/bin/env bash
# Author: Andy Zhou
# Date: 2013.8.2
Getnextday ()
{
# Date-d "2013-09-10 + 1 day" + % Y-% m-% d
Date-d "$1 + 1 day" + % Y % m % d
}
Getyearmonth ()
{
Date + % Y % m -- date = $1 # 1_date
}
Getday ()
{
Date + % d -- date = $1 # 1_date
}

Long_date ()
{
Date + % Y-% m-% d -- date = $1 # 1_date
}
Short_date ()
{
Date + % Y % m % d -- date = $1 # longdate
}
Long_yesterday ()
{
Date -- date = '1 day ago '+ % Y-% m-% d
}
Yesterday ()
{
Date -- date = '1 day ago '+ % Y % m % d
}
Long_today ()
{
Date + % Y-% m-% d
}
Today ()
{
Date + % Y % m % d
}
Now ()
{
Date '+ % Y-% m-% d % H: % M: % s'
}
Last_month ()
{
Date -- date = '1 month ago ''+ % Y % m'
}
Year ()
{
Date + % Y
}
Month ()
{
Date + % m
}
Sec2date ()
{
Date-d "1970-01-01 UTC $1 seconds" "+ % Y % m % d"
}
Sec2datetime ()
{
Date-d "1970-01-01 UTC $1 seconds" "+ % Y % m % d % H: % M: % S"
}

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.