views:

114

answers:

3

Hi,

I've made a bash script executing a PHP file:

#!/bin/bash
php upgrade_attendance.php refresh_daily_attendance 2010-10-01 2010-11-01 
php upgrade_attendance.php refresh_daily_attendance 2010-09-01 2010-10-01
php upgrade_attendance.php refresh_daily_attendance 2010-08-01 2010-09-01
php upgrade_attendance.php refresh_daily_attendance 2010-07-01 2010-08-01

etc... these dates have to go back until the end of the year. Now I can either simply have tons of lines in this script with those dates in there or have it count back until the end of this year (or one full year) from todays date

I just don't know how.

+1  A: 

This script:

#!/bin/bash
let YEAR=$(date +%-Y)
let MONTH=$(date +%-m)
let DAY=$(date +%-d)

for ((m = MONTH; m < 12; m++)); do
    php upgrade_attendance.php refresh_daily_attendance "$YEAR-$((m))-$DAY" "$YEAR-$((m+1))-$DAY"
done

Executes these commands:

php upgrade_attendance.php refresh_daily_attendance 2010-9-1 2010-10-1
php upgrade_attendance.php refresh_daily_attendance 2010-10-1 2010-11-1
php upgrade_attendance.php refresh_daily_attendance 2010-11-1 2010-12-1

I wasn't sure exactly what these dates mean, so there are probably some off-by-one errors.

John Kugelman
Can't you have something like `%02d` to add that extra zero in the front. I remember having a similar problem with the `value too great for base` error, but somehow the `%` fixed it. Just a thought; I don't use bash much.
vlad003
@vlad003: Just remove the dash from the date spec to get the leading zero: `+%m`
Dennis Williamson
A: 

Wow thanks for all your replies, I've ended up settling for something like this, it's been checked in, it's a little crude but does the job fine for my needs.

let YEAR=$(date +%-Y)
let MONTH=$(date +%-m)
let DAY=$(date +%-d)
for ((m = MONTH; m < 12 && m > 0; m--)); do
    php /srv/www/htdocs/$SCHOOL/tools/upgrade_attendance.php refresh_daily_attendance "$YEAR-$((m))-$DAY" "$YEAR-$((m+1))-$DAY"
done
edumike
+1  A: 

No need for complicated logic and arithmetic. Don't run it around midnight or time changes or the last few days of the month.

#!/bin/bash
for m in {0..14}
do
    beg=$(date -d "now - $((m+1)) months" "+%Y-%m-%d")
    end=$(date -d "now - $m months" "+%Y-%m-%d")
    echo php upgrade_attendance.php refresh_daily_attendance "$beg" "$end"
done

Remove the echo to make it work

Example:

php upgrade_attendance.php refresh_daily_attendance 2010-08-01 2010-09-01
php upgrade_attendance.php refresh_daily_attendance 2010-07-01 2010-08-01
php upgrade_attendance.php refresh_daily_attendance 2010-06-01 2010-07-01
php upgrade_attendance.php refresh_daily_attendance 2010-05-01 2010-06-01
php upgrade_attendance.php refresh_daily_attendance 2010-04-01 2010-05-01
php upgrade_attendance.php refresh_daily_attendance 2010-03-01 2010-04-01
php upgrade_attendance.php refresh_daily_attendance 2010-02-01 2010-03-01
php upgrade_attendance.php refresh_daily_attendance 2010-01-01 2010-02-01
php upgrade_attendance.php refresh_daily_attendance 2009-12-01 2010-01-01
php upgrade_attendance.php refresh_daily_attendance 2009-11-01 2009-12-01
php upgrade_attendance.php refresh_daily_attendance 2009-10-01 2009-11-01
php upgrade_attendance.php refresh_daily_attendance 2009-09-01 2009-10-01
php upgrade_attendance.php refresh_daily_attendance 2009-08-01 2009-09-01
php upgrade_attendance.php refresh_daily_attendance 2009-07-01 2009-08-01
php upgrade_attendance.php refresh_daily_attendance 2009-06-01 2009-07-01
Dennis Williamson