Shell finishing (===date) small script for command

Source: Internet
Author: User

(a) accept an input, judging is not a date, the user's input can be 2017-10-1 or 2017.10.1 or 2017/10/1


The shell code is as follows

#!/bin/bash


Read-p "Input:" Date

A= ' echo $date | Awk-f ' [./-] ' {print $ '/' $ '/' $ $} '

Date-d $a &>/dev/null

If [$?-eq 0];then

echo "OK"

Else

echo "No"

Fi


(ii) write the date into a text that determines the date of the year, respectively.

For example

[email protected] ding1]# cat File1

2017-10-1

2016-2-2

2017-1-1

[Email protected] ding1]#


The shell code is as follows

Str () {

# a= ' echo $line | TR '/.-'/'

A= ' echo $line | Awk-f "[./-]" ' {print $ "/" $ "/" $ $} "

Date-d $a +%j


}

#set-X

Cat File1 | While Read line

Do

Str

Done

#set +x


Summary of the simple date command:

Date-d < string >: Displays the date and time referred to by the self-character string. String before and after the double quotation marks, as if some do not add, specific case analysis

For example:

[Email protected] ding1]# date-d 2017-1-1 +%j #加%j indicates the day ordinal of the year

001

[Email protected] ding1]# date-d +%j

Date: Invalid Day "+%j"

[Email protected] ding1]#

In other words, a string must be specified after date-d.

[Email protected] ding1]# date-d 2017-1-1 #显示字符串所指的时间与日期

January 01, 2017 Sunday 00:00:00 CST

[Email protected] ding1]#

Date +%f Displays the current time, (year-month-day), for example:

[Email protected] ding1]# date +%f

2017-10-05

[Email protected] ding1]#

+%f is equivalent to +%y-%m-%d

For example:

[Email protected] ding1]# date +%y-%m-%d

17-10-05

[Email protected] ding1]# date +%y*%m*%d #中间的符号自己定义

17*10*05

[Email protected] ding1]#

For example I want to show the next month and the next year today

[Email protected]lhost ding1]# date-d +1month +%y-%m-%d

17-11-05

[Email protected] ding1]# date-d +1year +%f

2018-10-05

[Email protected] ding1]#

Date-s < string;: Sets the date and time according to the string, only the root permission can be set, the other can only be viewed. Double quotation marks around strings

[[Email protected] ding1]# Date

Thursday, October 05, 2017 10:21:52 CST

[[email protected] ding1]# date-s 2012-5-23 01:01:10

Date: Parameter ' 01:01:10 ' lacks a leading "+";

When the option is used to describe the date, any non-option parameter must appear as a "+"-guided string

Try executing "date--help" for more information.

[Email protected] ding1]# date-s "2012-5-23 01:01:10" #这个地方加上双引号

Wednesday, May 23, 2012 01:01:10 CST

[[Email protected] ding1]# Date

Wednesday, May 23, 2012 01:01:13 CST

[Email protected] ding1]#



This article is from the "It Life" blog, so be sure to keep this source http://dingxue.blog.51cto.com/12290895/1970420

Shell finishing (===date) small script for command

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.