1

I wrote a script to that works fine on Linux but fails on AIX 7.2. My script uses the command date with -d. The purpose of this script is to get the last day that is not a Saturday, Sunday or a holiday. To validate if a day is a holiday I perform a query passing a date in the format YYYY-MM-DD.

The script is this:

D_MINUS_ONE=

GetLastDay()
{
        current_day=""
        day_of_week=$(date -d "$1 -1 days" +'%w')
        if [ $day_of_week -eq 0 ]
        then
                current_day=$(date -d "$1 -3 days")
        else
                current_day=$(date -d "$1 -1 days")
        fi
        db2 -x "SELECT COUNT(*) FROM HOLIDAY WHERE DT_HOLIDAY='$(date -d "$current_day" +'%Y-%m-%d')'" | read lines
        if [ lines -gt 0 ]
        then
                GetLastDay $(date -d "$current_day" +'%Y-%m-%d')
        fi
        D_MINUS_ONE=$(date -d "$current_day" +'%Y-%m-%d')
}

GetLastDay $1

echo $D_MINUS_ONE
1

The -d is a GNU date extension (busybox and ast-open date also have one though work differently).

AIX comes with ksh93 though, so no need for GNU date to get the previous working day:

day=2016-01-01
previous_working_day=$(D=$day ksh93 -c '
  printf "%(%F)T" "$D, $(($(printf "%(%w)T" "$D") == 1 ? 3 : 1)) day ago"')
  • Stéphane, thank you. That's what I need to make it works on AIX. – Felipe Lemos Apr 4 at 20:31
0
D_MINUS_ONE=

GetLastDay()
{
        last_day=$(D=$1 ksh93 -c 'printf "%(%F)T" "$D, $(($(printf "%(%w)T" "$D") == 1 ? 3 : 1)) day ago"')
        db2 -x "SELECT COUNT(*) FROM HOLIDAY WHERE DT_HOLIDAY='$last_day'" | read lines
        if [ lines -gt 0 ]
        then
                GetLastDay $last_day
        fi
        D_MINUS_ONE=$last_day
}

GetLastDay $1

echo $D_MINUS_ONE

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.