使用ShellScript比较给定日期是一个月的最后一个工作日 [英] Compare a given date is last Business Day of a month using ShellScript

查看:72
本文介绍了使用ShellScript比较给定日期是一个月的最后一个工作日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文件具有以下记录;我使用Shell脚本-While循环读取这些文件.如果我遇到一个月的最后一个营业日,则需要跳过它们.

My File has below records ; im using Shell Script - While loop to read these files. In case if i encounter last business date of a month i need to skip them.

Name   date
David  09/30/2013
jack  10/01/2014
Mark  10/02/2014
John  10/13/2014
Daniel 10/30/2014
Rob    10/31/2014 

例如,在上述记录集中,我不得不跳过David和Rob,因为它们的Date值落在Sep&的最后一个工作日.十月.

For example in above record set i have to Skip David and Rob considering their Date values falls on last business day of Sep & Oct.

推荐答案

假定周六和周日都不是工作日,以下脚本将检查日期并报告是否为该月的最后一个工作日:

Assuming that Saturdays and Sundays are not business days, the following script checks the date and reports if it is the last business day of the month:

#!/bin/bash
days_to_add=(1 1 1 1 1 3 2)

seconds=$(date -d "$1" '+%s')
month1=$(date -d "@$seconds" '+%m')
day1=$(date -d "@$seconds" '+%w') # sun=0 sat=6
((seconds2=seconds + ${days_to_add[$day1]}*24*60*60))
month2=$(date -d "@$seconds2" '+%m')
[[ $month2 == $month1 ]] || echo "$1 is last business day of the month"

这里有一些示例显示了脚本的输出,除非日期是该月的最后一个工作日:

Here are some examples showing no output from the script unless the date is the last business day of the month:

$ bash script.sh 09/29/2013
$ bash script.sh 09/30/2013
09/30/2013 is last business day of the month
$ bash script.sh 10/30/2014
$ bash script.sh 10/31/2014
10/31/2014 is last business day of the month
$ bash script.sh 8/29/2014
8/29/2014 is last business day of the month

请注意,2014年8月29日不是一个月的最后一天,而是一个星期五,因此是该月的最后一个工作日.

Note that 8/29/2014 is not the last day of month but it is a Friday and is consequently the last business day of the month.

这已在 bash 下并使用GNU date 进行了测试.

This was tested under bash and using GNU date.

  • 使用 date ,将命令行中提供的日期转换为 seconds (自纪元以来的秒数), month1 (月)和 day (星期几,星期天= 0).

  • Using date, the date supplied on the command line is converted to seconds (seconds since epoch), month1 (the month), and day (the day of the week with Sunday=0).

接下来,我们确定下一个工作日.这是给定日期之后的1天,除非给定日期为星期五或星期六. bash 数组 days_to_add 用于确定距下一个工作日的天数.然后,将许多天时间每天24小时乘以每小时60分钟乘以每分钟60秒加 seconds 来确定 seconds2 ,这是自下一个业务开始到现在的秒数一天.

Next, we determine the next business day. This is 1 day after the given date unless the given date falls on Friday or Saturday. The bash array days_to_add is used to determine how many days until the next business day. Then, that many days times 24 hours per day times 60 minutes per hour times 60 seconds per minute is added seconds to determine seconds2 which is the seconds since epoch for the next business day.

最后,确定下一个工作日发生的月份 month2 ,并将其与给定日期的月份 month1 进行比较.如果它们不同,则会打印一条消息.

Finally, the month on which the next business day occurs is determined, month2, and compared with the month of the given date, month1. If they differ, a message is printed.

这将循环输入文件以及日期为每月最后一天的任何行:

This will loop over your input file and while any line whose date is the last day of the month:

#!/bin/bash
days_to_add=(1 1 1 1 1 3 2)

not_last() {
    seconds=$(date -d "$1" '+%s')
    month1=$(date -d "@$seconds" '+%m')
    day1=$(date -d "@$seconds" '+%w') # sun=0 sat=6
    ((seconds2=seconds + ${days_to_add[$day1]}*24*60*60))
    month2=$(date -d "@$seconds2" '+%m')
    [[ $month2 == $month1 ]]
}

while read name mmddyy
do
    if [[ $mmddyy == date ]] || not_last "$mmddyy"
    then
        printf "%s\t%s\n" "$name" "$mmddyy"
    fi
done <"$1"

如果您的示例输入位于名为 input 的文件中:

If your sample input is in a file called input:

$ bash script.sh input
Name    date
jack    10/01/2014
Mark    10/02/2014
John    10/13/2014
Daniel  10/30/2014

请注意,根据需要,David和Rob被跳过了.

Note that, as desired, David and Rob were skipped.

date 实用程序可识别的日期格式取决于语言环境.如果日期格式和区域设置不匹配,则必须执行某些操作.假设我们有以下日期字符串:

The format of the dates recognized by the date utility depends on locale. If the date format you have and your locale do not match, then something must be done. Suppose we have this date string:

datestring="30/01/2014"

可以使用 awk 将其转换为mm/dd/yyyy,如下所示:

This can be converted to mm/dd/yyyy using awk as follows:

datestring=$(echo "$datestring | awk -F/ -v OFS=/ '{print $2,$1,$3})

这篇关于使用ShellScript比较给定日期是一个月的最后一个工作日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆