From: Michael Paoli on
On May 19, 2:20 pm, underh20 <underh20.scubadiving(a)gmail.com> wrote:
> Our server is running Solaris 10. I need  to run a cron job at 6am on
> last Sunday of every month starting from the last Sunday this month in
> May.
>
> The current cron job below runs on every Sunday.  How could we modify
> it to run on last Sunday only in each month for the next 3 years ?
> Thanks, Bill
>
> 0 6 * * 0   /usr/bin/script

0 6 * * 0 if [ `date +%d` = `cal | sed -ne ':t;$!{;N;bt;};${s/\n *
$//;s/^.*\n\(..\)[^\n]*$/\1/p;};'` ]; then ...; else :; fi

May vary somewhat depending upon one's output format of cal(1).
"for the next 3 yeras" has been left as an exercise.
From: Ike Naar on
In article <d14c103d-ae48-4b84-8c0d-1e6c6065a341(a)k25g2000prh.googlegroups.com>,
Michael Paoli <michael1cat(a)yahoo.com> wrote:
>On May 19, 2:20�pm, underh20 <underh20.scubadiving(a)gmail.com> wrote:
>> Our server is running Solaris 10. I need �to run a cron job at 6am on
>> last Sunday of every month starting from the last Sunday this month in
>> May.
>>
>> The current cron job below runs on every Sunday. �How could we modify
>> it to run on last Sunday only in each month for the next 3 years ?
>> Thanks, Bill
>>
>> 0 6 * * 0 � /usr/bin/script
>
>0 6 * * 0 if [ `date +%d` = `cal | sed -ne ':t;$!{;N;bt;};${s/\n *
>$//;s/^.*\n\(..\)[^\n]*$/\1/p;};'` ]; then ...; else :; fi
>
>May vary somewhat depending upon one's output format of cal(1).
>"for the next 3 yeras" has been left as an exercise.

Nice!
Just curious: why the `` else :; '' part?
Anything wrong with `` if [ ... ]; then ...; fi '' ?

Here's one that's a bit shorter:
0 6 * * 0 if cal | grep "^`date +%d` " >/dev/null; then ... ; fi
From: Ike Naar on
In article <htg04f$a96$1(a)news.eternal-september.org>,
Ike Naar <ike(a)localhost.claranet.nl> wrote:
>0 6 * * 0 if cal | grep "^`date +%d` " >/dev/null; then ... ; fi

Sorry, brain fart; forget it.
From: Richard B. Gilbert on
Keith Thompson wrote:
> underh20 <underh20.scubadiving(a)gmail.com> writes:
> [...]
>> Please critique my program below. I'd like to get feedback from you
>> experts out there :-)
>> Thanks, Bill
>>
>>
>> < body of /usr/bin/script>
>
> It's not really called "/usr/bin/script", is it? There's a standard
> command called "script", and it typically lives in /usr/bin.
>
>> #!/bin/sh
>>
>> LAST_SUNDAY=`cal | cut -c1-2|grep -v "^$"|tail -1`
>
> The above didn't work on my system because the output of "cal"
> includes leading and trailing blanks:
>
> $ cal | cat -A
> June 2010 $
> Su Mo Tu We Th Fr Sa$
> 1 2 3 4 5$
> 6 7 8 9 10 11 12$
> 13 14 15 16 17 18 19$
> 20 21 22 23 24 25 26$
> 27 28 29 30 $
> $
> $ cal | cut -c1-2 | cat -A
> $
> Su$
> $
> 6$
> 13$
> 20$
> 27$
> $
>
> Rather than grep -v "^$", try grep '[0-9]' to grab just lines containing
> digits:
>
> LAST_SUNDAY=`cal | cut -c1-2 | grep '[0-9]' | tail -1`
>
>> DAY=`date +%d`
>> if [$LAST_SUNDAY = $DAY]
>
> You need spaces around the square brackets:
>
> if [ $LAST_SUNDAY = $DAY ]
>
>> then
>> #
>> # <body of codes to be executed>
>> #
>> exit 0
>> else
>> echo "EXIT!! Today is not last Sunday of month"
>> fi
>
> Suggestion: Try running code before you post it.
>

The late Carl J. Lydick was famous for his attacks on wrong answers and
broken code. If you posted something containing an error, he would
simply reply "Did you test it?"

Almost invariably, the poster had not only not tested it, it didn't
work!