Loop from start date to end date in Mac OS X shell script
The date function on OS X (Snow Leopard) does not have the --date option like the GNU version and I am not able to figure out how to get the equivalent of the following on OS X:
startdate=2010-01-01
enddate=2010-01-31
foldate="$startdate"
until [ "$foldate" == "$enddate" ]
do
# do something with the date here - like pass it as a parameter to a command
foldate=`/bin/date --date "$foldate 1 day" +%Y-%m-%d`
done
SOLVED with answers from SiegeX:
startdate=2010-01-01
enddate=2010-01-31
sDateTs=`date -j -f "%Y-%m-%d" $startdate "+%s"`
eDateTs=`date -j -f "%Y-%m-开发者_开发问答%d" $enddate "+%s"`
dateTs=$sDateTs
offset=86400
while [ "$dateTs" -le "$eDateTs" ]
do
date=`date -j -f "%s" $dateTs "+%Y-%m-%d"`
printf '%s\n' $date
dateTs=$(($dateTs+$offset))
done
Read your date's manpage by running man date
and look at the -v
option. Here is an excerpt:
-v Adjust (i.e., take the current date and display the result of the adjustment; not actually set the date) the second, minute, hour, month day, week day, month or year according to val. If val is preceded with a plus or minus sign, the date is adjusted forwards or backwards according to the remaining string, otherwise the relevant part of the date is set. The date can be adjusted as many times as required using these flags. Flags are processed in the order given.
精彩评论