Replace integer string in a variable based on month?

Hi Folks -

Linux Version = Linux 2.6.39-400.128.17.el5uek x86_64

I have a process that determines the start and end load periods for an Oracle data load process.

The variables used are as follows follows:

They are populated like such:

However, the load requires the month to be the 3 digit format rather than number format. Therefore, to get around it, I am using a cut methodology but I w wondering if I'm over complicating things and there is an easier way?

GetLoadPOVValues () {
    
    #::-- Usage --::#
        # Param 1 = Required Year digits [i.e. 2 or 4]
        # Param 2 = Number of periods to load [i.e. YTD or 1 or 2 etc]
        
        # Examples are based on a November 2018 run-time
        # Example = GetLoadPOVValues "2" "1" says use 2 digit year [18] and _STARTPOV & _ENDPOV are the same [Oct-2018] since loading only 1 month
        # Example = GetLoadPOVValues "4" "YTD" says use 4 digit year [2018] and _STARTPOV is Jan-2018 and _ENDPOV is Oct-2018
        
    _REGEXP='^[0-9]+$'
    _DIGIT="$1"; [ "${_DIGIT}" -eq "2" ] && _YR="y" || _YR="Y"
    _NUMPD="$2"
    if ! [[ "${_NUMPD}" =~ "${_REGEXP}" ]]
    then
        _STARTPOV="1-$(date +%${_YR})"
    else
        _STARTPOV=`date -d "$(date +%Y-%m-1) -${_NUMPD} month" +%-m-%${_YR}`
    fi
    _ENDPOV=`date -d "$(date +%Y-%m-1) -1 month" +%-m-%${_YR}`
    
    _M="$( cut -d '-' -f 1 <<< "$_ENDPOV" )"; GetMonth "${_M}"; _YR="$( cut -d '-' -f 2 <<< "$_ENDPOV" )"; _ENDPOV="${_M}-${_YR}"
    _M="$( cut -d '-' -f 1 <<< "$_STARTPOV" )"; GetMonth "${_M}"; _YR="$( cut -d '-' -f 2 <<< "$_STARTPOV" )"; _STARTPOV="${_M}-${_YR}"

}

GetMonth () {
    _NUM="$1"
    [[ "${_NUM}" == "01" ]] && _M="Jan" || [[ "${_NUM}" == "1" ]]  && _M="Jan"
    [[ "${_NUM}" == "02" ]] && _M="Feb" || [[ "${_NUM}" == "2" ]]  && _M="Feb"
    [[ "${_NUM}" == "03" ]] && _M="Mar" || [[ "${_NUM}" == "3" ]]  && _M="Mar"
    [[ "${_NUM}" == "04" ]] && _M="Apr" || [[ "${_NUM}" == "4" ]]  && _M="Apr"
    [[ "${_NUM}" == "05" ]] && _M="May" || [[ "${_NUM}" == "5" ]]  && _M="May"
    [[ "${_NUM}" == "06" ]] && _M="Jun" || [[ "${_NUM}" == "6" ]]  && _M="Jun"
    [[ "${_NUM}" == "07" ]] && _M="Jul" || [[ "${_NUM}" == "7" ]]  && _M="Jul"
    [[ "${_NUM}" == "08" ]] && _M="Aug" || [[ "${_NUM}" == "8" ]]  && _M="Aug"
    [[ "${_NUM}" == "09" ]] && _M="Sep" || [[ "${_NUM}" == "9" ]]  && _M="Sep"
    [[ "${_NUM}" == "10" ]] && _M="Oct"
    [[ "${_NUM}" == "11" ]] && _M="Nov"
    [[ "${_NUM}" == "12" ]] && _M="Dec"
}

GetLoadPOVValues "2" "2"

Thank you!!

_MONTHS=(nul Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
_M=${_MONTHS[${_STARTPOV%-*}]}
_STARTPOV=${_M}-${_STARTPOV#*-}

Same for _ENDPOV

The 'nul' is there as arrays index from zero, otherwise you will have to subtract one from the index of months.

I don't think you need to worry about a zero prefix as your code seems to be ensuring the month numbers are not padded by zero or space, but if I'm wrong, use this instead:

_MONTHS=(nul Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
_M=${_MONTHS[10#${_STARTPOV%-*}]}
_STARTPOV=${_M}-${_STARTPOV#*-}

If you want to change your month names depending on your locale, consider using this for your month array:

_MONTHS=( nul $(locale abmon|tr ';' ' '))

Andrew

Your function could be boiled down to

GLP()   { IFS=";" T=($(locale abmon))
          IFS=- read EM EY <<< $(date -d "$(date +%Y-%m-1) -1 month" +%-m-%Y)
          IFS=- read SM SY <<< $(date -d "$(date +%Y-%m-1) -$(($2+0?$2:EM)) month" +%-m-%Y)
          _ENDPOV=${T[EM-1]}-${EY:4-$1}
          _STARTPOV=${T[SM-1]}-${SY:4-$1}
        }


$ GLP 4 2
$ echo $_STARTPOV, $_ENDPOV;
Sep-2018, Okt-2018
$ GLP 4 YTD
$ echo $_STARTPOV, $_ENDPOV;
 Jan-2018, Okt-2018
$ GLP 2 YTD
$ echo $_STARTPOV, $_ENDPOV;
Jan-18, Okt-18

You might want to add some error checking, though.

Thank you, Rudi!!

I'm however getting the following errors:

Unfortunately, there is no line 81 nor line 82 in my proposal.

Hi Rudi -

Sorry, these are the line.

Hmmm - how do you expect people to debug any code without giving any context? What's your code? How is it called? What's the contents of the local / temporary variables? What's your shell version?

Rudi -

I'm literally using your code you gave me. Version in first post.

Linux Version = Linux 2.6.39-400.128.17.el5uek x86_64

#!/bin/bash

GLP() { 
    IFS=";" T=($(locale abmon))
    IFS=- read EM EY <<< $(date -d "$(date +%Y-%m-1) -1 month" +%-m-%Y)
    IFS=- read SM SY <<< $(date -d "$(date +%Y-%m-1) -$(($2+0?$2:EM)) month" +%-m-%Y)
    _ENDPOV=${T[EM-1]}-${EY:4-$1}
    _STARTPOV=${T[SM-1]}-${SY:4-$1}
}

GLP "2" "2"

echo $_ENDPOV
echo $_STARTPOV

Run with -x (xtrace) option set. What's your shell version?

Hi Rudi -

It's as follows:

Thanks!

------ Post updated at 11:49 AM ------

Interestingly enough, it works fine on this version of shell:

Any thoughts?

Yes - 3.2.25(1) is too old.

:frowning: Thank you.