Use the shell to get the date n days before the specified date _linux shell

Source: Internet
Author: User
Tags current time

First, create test files test.sh

#!/bin/sh
./etc/profile

# Parameters:
# Args[0], data date, date format Yyyy-mm-dd
# Take 30 days before the date

function Get_date_ 30daysbefore ()
{
 sec= ' date-d $ +%s '
 sec_30daysbefore=$ ((sec-86400*30))
 days_before= ' date-d @$ Sec_30daysbefore +%f '
 echo $days _before
}

If [$# = 1]; then today=$1 dates_30=
 ' Get_date_ 30daysbefore '
else
 today= ' date-d -1days ' +%y-%m-%d ' dates_30= ' date-d
 -30days
' +%y-%m-%d ' Fi

echo $today
echo $dates _30

Ii. Non-parametric operation

# # without parameters, starting from the current time
$./test.sh

2016-10-28
2016-09-29

Third, the transfer of the parameters of the operation

$./test.sh 2015-10-29
2015-10-29
2015-09-29

Summarize

The above is the use of shell to get the specified date of the date of the full content of N days, I hope the content of this article for everyone's study or work can help, if there is doubt you can message exchange.

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.