Conditional Cron

Hi,

The following test in cron fails. Any clue?

I want it to run last day before end of month

34 13 * * * [ `date +'\%d'` -eq `cal | grep -v "^$" | tail -1 | awk '{print $NF-1}'` ] && /export/myshell.sh

Regards

The backslash not required...

[ `date '+%d'` -eq `cal | grep -v "^$" | tail -1 | awk '{print $NF-1}'` ] && do....what required..

The cron does not give any error, but i dont get the output file either...

regards

First, the backslash is required. From the crontab man page:

Some versions of cron are smart enough to remove that blackslash prior to passing it to the shell. Others just leave it there. So the shell will see:
[ \30 -eq 30 ]
Most shells will get that right but some versions of the bourne shell will not. You are probably using Solaris which has both behaviors. My advice is to put shell scripting into a shell script which is executed by a modern shell. If you must get this to work, use eval with the date command

34 13 * * * [ `eval date +'\%d'` -eq `cal | grep -v "^$" | tail -1 | awk '{print $NF-1}'` ] && /export/myshell.sh

running this in ksh seems to work -

[ $(TZ=GMT-48 date +%d) -eq "01" ] && echo true

Of course, you will need to make adjustments if you are far from England.

Basically, sets the date 48 hours ahead and does the date. Today, the 30th, comes up with 01, since march has 30 days.

The cal routine would not work on months where the last day of the month was on Sunday, since that would be the first field, and $NF-1 would be the entire line (the last day), and not the day before.