开发者

Bash script - How to take todays date and count back monthly for a year

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.ph开发者_运维问答p 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.


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


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜