OS X (Snow Leopard) 上的日期函数没有像 GNU 版本那样的 --date 选项,我无法弄清楚如何在 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
解决了 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
最佳答案
通过运行 man date
阅读你的日期的联机帮助页并查看 -v
选项。以下是摘录:
-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.
关于macos - 在 Mac OS X shell 脚本中从开始日期循环到结束日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4434782/