cancel
Showing results for
Did you mean:

SOLVED
Go to solution

## check date

I would like to write a script as below , can advise what can i do .

if this month is Jan , May , July and today is last day of month ( 28th, 29th , 30th or 31th )

then do xxx

if this month is Feb , Mar , Aug and today is last day of month ( 28th, 29th , 30th or 31th )

then do yyy

end.

can advise how to write this script ? thx
6 REPLIES
HPE Pro
Solution

## Re: check date

You can use Arturo's trick to find the last day of the month:

if [[ \$(cal) = *\$(date +%d) ]]; then # last day of month
case \$(date \$b) in
Jan|May|Jul) xxx;;
Feb|Mar|Aug) yyy;;
esac
fi

## Re: check date

I changed today as 31Dec and the script as below .

=================================
if [[ \$(cal) = *\$(date +%d) ]];
echo testing
case \$(date \$b) in
Jan|May|Jul) echo test1 ;;
Feb|Mar|Dec) echo test2 ;;

esac
fi
==================================

The output is "testing" only ,
I think it check today is last day , but do not recognize this month is Dec , can advise how to change it ? thx

Esteemed Contributor

## Re: check date

The folowing will work for most shells

case \$(date |awk '{ print \$2 } ' ) in
Jan|May|Jul) echo test1 ;;
Feb|Mar|Dec) echo test2 ;;
esac
HPE Pro

## Re: check date

>The output is "testing" only, but do not recognize this month is Dec

Oops, a typo, \$b vs %b:
if [[ \$(cal) = *\$(date +%d) ]]; then # last day of month
case \$(date %b) in
Jan|May|Jul) test1;;
Feb|Mar|Aug) test2;;
esac
fi

>F Verschuren: The following will work for most shells

Why would you want to use awk(1) if date(1) will do the right thing, if you do it right. :-)

HPE Pro

## Re: check date

Oops again, that should be:
case \$(date +%b) in