Bugfix: Re: [linux-elitists] cron (and other tricks): nth day, last day scripts

Karsten M. Self kmself@ix.netcom.com
Sat Apr 9 20:21:25 PDT 2005


on Mon, Mar 21, 2005 at 05:18:49PM -0800, Karsten M. Self (kmself@ix.netcom.com) wrote:
> There's a recurring question of how to run cron jobs at specific
> intervals -- the nth weekday of the month, or the last given weekday of
> the month.
> 
> Playing with the 'date' command gives several useful tests which can be
> run.  In a bash 'if/then' clause, actions can be taken.  Returning a
> true/false value allows the result itself to be tested directly and
> concisely on a crontab entry.
> 
> Both scripts make use of a bashism, the $() notation, keep in mind if
> porting to other shells.


Problem:  for day-of-month < 10, 'date +%d' outputs a zero-padded date.
Which bash's arithetic evaluation $(( <expr> )) can't handle.

Soution:  Use 'date +%-d' instead.  The standard printf() formatting
directives are supported, according to docs.
 

------------------------------------------------------------------------
nthday
------------------------------------------------------------------------
#!/bin/bash
# Karsten M. Self Mon Mar 21 16:57:30 PST 2005
# Free use, distribution, and modification with no restrictions, granted.
# NO WARRANTY
# ------------------------------------------------------------------------
# Test if today is the nth weekday of the month.
# ------------------------------------------------------------------------

export PATH=/usr/bin:/bin

NTHDAY=$1

if [ $(( ( ( $( date +%-d) + 1 ) /7 ) )) -eq $NTHDAY ]
    then true
    else false
fi
------------------------------------------------------------------------


------------------------------------------------------------------------
lastday
------------------------------------------------------------------------
#!/bin/bash
# Karsten M. Self Mon Mar 21 16:57:30 PST 2005
# Free use, distribution, and modification with no restrictions, granted.
# NO WARRANTY
# ------------------------------------------------------------------------
# Test if today is the last given weekday of month
# ------------------------------------------------------------------------

export PATH=/usr/bin:/bin

if [ $( date -d '+ 7 days' +%-d) -lt $( date +%-d ) ]
    then true
    else false
fi
------------------------------------------------------------------------


Peace.

-- 
Karsten M. Self <kmself@ix.netcom.com>        http://kmself.home.netcom.com/
 What Part of "Gestalt" don't you understand?
   A guide to GNU/Linux books:
     http://kmself.home.netcom.com/Linux/FAQs/linux-books.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://allium.zgp.org/pipermail/linux-elitists/attachments/20050409/48479296/attachment.pgp 


More information about the linux-elitists mailing list