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
bash
OSX has (if it even has bash), but if it does you should wrap your commands in$()
instead of backticks likesDateTs=$(date -j -f ...)
and use double brackets[[
instead of a single[
so you can dowhile [[ "$dateTS" < "$eDateTs" ]]
=
than==
in[
, as use of==
is an extension not all baseline-POSIX shells support, whereas=
is defined by the POSIX standard fortest
.