Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the date Operation in Shell script

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to understand the date operation in Shell script". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "how to understand the date operation in Shell script"!

Date plus or minus

seconds increase/decrease

[root@ecs-centos-7 ~]# date +'%Y-%m-%d %H:%M:%S' -d "70 second 2020-10-20 12:00:00" 2020-10-20 12:01:10 [root@ecs-centos-7 ~]# date +'%Y-%m-%d %H:%M:%S' -d "-70 second 2020-10-20 12:00:00" 2020-10-20 11:58:50

Minute increase/decrease

[root@ecs-centos-7 ~]# date +'%Y-%m-%d %H:%M:%S' -d "10 minute 2020-10-20 12:00:00" 2020-10-20 12:10:00 [root@ecs-centos-7 ~]# date +'%Y-%m-%d %H:%M:%S' -d "-10 minute 2020-10-20 12:00:00" 2020-10-20 11:50:00

hour increase/decrease

[root@ecs-centos-7 ~]# date +'%Y-%m-%d %H:%M:%S' -d "1 hour 2020-10-20 12:00:00" 2020-10-20 13:00:00 [root@ecs-centos-7 ~]# date +'%Y-%m-%d %H:%M:%S' -d "-1 hour 2020-10-20 12:00:00" 2020-10-20 11:00:00

day increase or decrease

[root@ecs-centos-7 ~]# date +'%Y-%m-%d %H:%M:%S' -d "3 day 2020-10-20 12:00:00" 2020-10-23 12:00:00 [root@ecs-centos-7 ~]# date +'%Y-%m-%d %H:%M:%S' -d "-3 day 2020-10-20 12:00:00" 2020-10-17 12:00:00

weekly increase or decrease

[root@ecs-centos-7 ~]# date +'%Y-%m-%d %H:%M:%S' -d "1 week 2020-10-20 12:00:00" 2020-10-27 12:00:00 [root@ecs-centos-7 ~]# date +'%Y-%m-%d %H:%M:%S' -d "-1 week 2020-10-20 12:00:00" 2020-10-13 12:00:00

monthly increase and decrease

[root@ecs-centos-7 ~]# date +'%Y-%m-%d %H:%M:%S' -d "2 month 2020-10-20 12:00:00" 2020-12-20 12:00:00 [root@ecs-centos-7 ~]# date +'%Y-%m-%d %H:%M:%S' -d "-2 month 2020-10-20 12:00:00" 2020-08-20 12:00:00

Annual increase or decrease

[root@ecs-centos-7 ~]# date +'%Y-%m-%d %H:%M:%S' -d "1 year 2020-10-20 12:00:00" 2021-10-20 12:00:00 [root@ecs-centos-7 ~]# date +'%Y-%m-%d %H:%M:%S' -d "-1 year 2020-10-20 12:00:00" 2019-10-20 12:00:00

Year, Month, Hour, Minute, Minute, Increase, Decrease

Year, Month, Hour, Minute, Minute

[root@ecs-centos-7 ~]# date +'%Y-%m-%d %H:%M:%S' -d "1 year 2 month 1 week 3 hour -10 minute 40 second 2020-10-20 12:00:00" 2021-12-27 14:50:40

What day of the month?

[root@ecs-centos-7 ~]# date +%Y%m%d 20201023 [root@ecs-centos-7 ~]# date +%d 23 [root@ecs-centos-7 ~]# date +%d -d "20201013" 13

date +%d command is to calculate the day of the month the current date (October 23, 2020) is

date +%d -d "20201013" command is to calculate the day of the month on October 13, 2020

Week of the year

[root@ecs-centos-7 ~]# date +%Y%m%d 20201023 [root@ecs-centos-7 ~]# date +%V 43 [root@ecs-centos-7 ~]# date +%V -d "20201005" 41

In the above example, the date +%V command calculates the week of the year for the current date, Monday is the first day of the week, the value range is 01-53, date +%V -d "20201005" command calculates the week of the year for the specified date ( 20201005 )

Day of the week

[root@ecs-centos-7 ~]# date +%Y%m%d 20201023 [root@ecs-centos-7 ~]# date +%u 5 [root@ecs-centos-7 ~]# date +%u -d "20201004" 7

In the above example, the date +%u command calculates the day of the week for the current date. The value range is 1 - 7, indicating Monday to Sunday respectively. The result is 5, indicating that the current day is Friday. The date +%u -d "20201004" command calculates the day of the week for the specified date ( 20201004 ). The result is 7, indicating that October 4, 2020 is Sunday.

[root@ecs-centos-7 ~]# date +%Y%m%d 20201023 [root@ecs-centos-7 ~]# date +%w 5 [root@ecs-centos-7 ~]# date +%w -d "20201004" 0

In the above example,date +%w command is also to calculate the current date is the day of the week, the value range is 0 - 6 , 0 represents Sunday, 1 - 6 represents Monday to Saturday respectively

Command date +%w -d "20201004" is to calculate the day of the week the specified date ( 20201004 ) is, the result is 0, indicating that October 4, 2020 is Sunday

First and last day of month

[root@ecs-centos-7 ~]# date +"%Y-%m-%d" -d "-$(($(date +%d -d '2020-10-20') -1 )) days 2020-10-20" 2020-10-01 [root@ecs-centos-7 ~]# date +"%Y-%m-%d" -d "-$(($(date +%d -d '2020-10-20') )) days +1 month 2020-10-20" 2020-10-31

In the example above, the command date +"%Y-%m-%d" -d "-$(($(date +%d -d '2020-10- 20')-1 )) days 2020-10-20" calculates the first day of the month in the date 2020-10-20, resulting in 2020-10-01

The command date +"%Y-%m-%d" -d "-$(($(date +%d -d '2020-10- 20'))) days +1 month 2020-10-20" is the last day of the month in the calculated date 2020-10-20, resulting in 2020-10-31

Date and UTC time conversion

Date to UTC Time

UTC time is an integer, converting the date to UTC time is more convenient for various calculations.

[root@ecs-centos-7 ~]# date +%s 1603443806 [root@ecs-centos-7 ~]# date +%s -d "2020-10-20 12:00:00" 1603166400 [root@ecs-centos-7 ~]# date +%s -d "2020-10-20 00:00:00" 1603123200 [root@ecs-centos-7 ~]# date +%s -d "2020-10-20" 1603123200 [root@ecs-centos-7 ~]# date +%s -d "2020-10-01" 1601481600

In the example above, the date +%s command converts the current time to UTC, and the date +%s -d "2020-10-20 12:00:00" command converts 2020-10-20 12:00:00 to UTC

UTC Time to Date

[root@ecs-centos-7 ~]# date +%s -d "2020-10-20 12:00:00" 1603166400 [root@ecs-centos-7 ~]# date -d "@1603166400" Tuesday, October 20, 2020 12:00:00 CST

The command date +%s -d "2020-10-20 12:00:00" is used to calculate UTC time of 2020-10-20 12:00:00, mainly for comparison with UTC time to date later.

Command date -d "@1603166400" is the date corresponding to UTC time ( 1603166400 ), the calculation result is Tuesday, October 20, 2020 12:00:00 CST The date parameter of the previous command is consistent

Date Comparison Size

shell direct comparison date is equivalent to string comparison, you can first convert the date to UTC time and then compare, the following is a date comparison test script (t.sh), execution needs to pass in two dates to be compared

#!/ bin/bash date1=$(date +%s -d "$1") date2=$(date +%s -d "$2") if [[ ${date1} -lt ${date2} ]]; then echo " $1

< $2 " elif [[ ${date1} -eq ${date2} ]]; then echo " $1 = $2 " else echo " $1 >

$2 " fi

Execute the test script and the results are as follows:

[root@localhost shell_test]# ./ t.sh "20201020" "20201020" 20201020 = 20201020 [root@localhost shell_test]# ./ t.sh "20201020" "20201021" 20201020

< 20201021 [root@localhost shell_test]# ./t.sh "20201020" "20201019" 20201020 >

20201019 [root@localhost shell_test]# [root@localhost shell_test]# ./ t.sh "2020-10-20 00:00:01" "2020-10-20 00:00:00" 2020-10-20 00:00:01 > 2020-10-20 00:00:00 [root@localhost shell_test]# ./ t.sh "2020-10-20 00:00:01" "2020-10-20 00:00:01" 2020-10-20 00:00:01 = 2020-10-20 00:00:01 [root@localhost shell_test]# ./ t.sh "2020-10-20 00:00:01" "2020-10-20 00:00:10" 2020-10-20 00:00:01 < 2020-10-20 00:00:10 [root@localhost shell_test]#

As can be seen from the results, the date parameter passed in by t.sh script can be only the year, month, day, hour, minute and second, and the date format can be 2020-10- 20 format or 2020-10-20 00:00:00 format.

date loop

In the work, often have to deal with logs within a period of time, need to traverse the date, and then do processing on the daily log, the following test script (t.sh) is the test date traversal, the script content is as follows:

#!/ bin/bash #start date begin =$1 #end date end date =$2 #next day date for cycling nextdate=${begin} #next day utc time for comparison date nextdate =$(date +%s -d "${nextdate}") #end date UTC time endc =$(date +%s -d "${end date}") while [ ${nextutc} -le ${endutc} ] do echo "process ${nextdate} log... " #Calculate the date of the next day nextdate=$(date -d "${nextdate} 1 day" +%Y%m%d) #Calculate the utc time of the next day nextutc=$(date +%s -d "${nextdate}") done

Execute the test script and the results are as follows:

[root@localhost shell_test]# ./ t.sh "20201020" "20201023" process 20201020 log... process 20201021 log... process 20201022 log... process 20201023 log... At this point, I believe that everyone has a deeper understanding of "how to understand the date operation in Shell script," so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report