Converting Month into Integer

okay so i run an openssl command to get the date of an expired script. Doing so gives me this:

enddate=Jun 26 23:59:59 2012 GMT

Then i cut everything out and just leave the month which is "Jun"
Now the next part of my script is to tell the user if the the certificate is expired or not and to do that i use an if statement in which it looks like this:

if [ $exp_year -lt $cur_year && $exp_month -lt $cur_month ]; then
          echo "" 
              echo "Certificate is still valid until $exp_date"
               echo "" 
else
              echo ""
              echo "Certificate has expired on $exp_date, please renew."
               echo ""
fi

I can't figure out how to convert the month into an integer to even do the comparison. I thought of doing the brute force way which is this:

Jan=01
Feb=02
Mar=03
...
  

Clearly that's a terrible way to do it. Does anyone know what i can do?

What OS and shell? Or do you have GNU date?

Well I am running the script on a Solaris OS and the script is in bash

MON="Oct"
M=1
for X in Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
do
        [ "$MON" = "$X" ] && break
        M=`expr $M + 1`
done

echo "Month number is $M"
$ echo "Jan" | awk 'BEGIN{months="  JanFebMarAprMayJunJulAugSepOctNovDec"}{print index(months,$0)/3}' 
1
$ echo "Feb" | awk 'BEGIN{months="  JanFebMarAprMayJunJulAugSepOctNovDec"}{print index(months,$0)/3}' 
2
$ echo "Dec" | awk 'BEGIN{months="  JanFebMarAprMayJunJulAugSepOctNovDec"}{print index(months,$0)/3}' 
12

3 Likes

Off-topic. The "if" test condition is dubious and I believe will always give the wrong result.
It needs to be something like this (assuming that the certificate expires at the end of the calendar month).

if [ \( $exp_year -gt $cur_year \) -o \( $exp_year -eq $cur_year -a $exp_month -ge $cur_month \) ]; then

I would suggest using a simple case statement. It's not especially clever, but it's tailor-made for this scenario.

case $month_name in
    Jan) month_number=1;;
    Feb) month_number=2;;
    Mar) month_number=3;;
esac

Regards,
Alister