Compare a given date is last Business Day of a month using ShellScript

人走茶凉 提交于 2020-01-25 11:38:15

问题


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 

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.


回答1:


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

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.

This was tested under bash and using GNU date.

How it works

  • 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).

  • 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.

  • 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.

Use in a loop over input

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"

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

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

Handling other date formats

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"

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

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


来源:https://stackoverflow.com/questions/26330563/compare-a-given-date-is-last-business-day-of-a-month-using-shellscript

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!