从开始日期循环结束在Mac OS X中的shell脚本日期 [英] Loop from start date to end date in Mac OS X shell script

查看:1651
本文介绍了从开始日期循环结束在Mac OS X中的shell脚本日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在OS X(雪豹)的日期函数没有像GNU版本的--date选项,我无法弄清楚如何获得OS X下的等价的:

  STARTDATE = 2010-01-01
ENDDATE = 2010-01-31
foldate =$ STARTDATE直到[$ foldate==$结束日期]

    #做的东西日期在这里 - 像它作为参数传递给命令
    foldate =`/斌/日期--date$ foldate1天+%Y-%M-%D`
DONE

与答案来自解决SiegeX:

  STARTDATE = 2010-01-01
ENDDATE = 2010-01-31sDateTs =`日期-j -f%Y-%M-%D$ STARTDATE+%的`
eDateTs =`日期-j -f%Y-%M-%D$结束日期+%的`
dateTs = $ sDateTs
偏移量= 86400而[$ dateTs-le$ eDateTs]

  日期=`日期-j -f%S$ dateTs+%Y-%M-%D`
  printf的'%s的\\ n'$日期
  dateTs = $(($ dateTs + $偏移))
DONE


解决方案

运行男人日期阅读你的约会的手册页,并期待在 -v 选项。下面是一个摘录:


  

-v 调整(即走
  当前的日期和显示的结果
  调整;实际上没有设置
               日期)第二,分钟,小时,月日,星期几,
  根据VAL一个月或一年。如果
               VAL是一个加号或减号pceded $ P $,日期是
  向前或向后调整
  根据
               到其余的串,否则的相关部分
  日期设置。日期可以
               作为使用这些标志需要调整多次。标志
  在给定的顺序进行处理。


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.

这篇关于从开始日期循环结束在Mac OS X中的shell脚本日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆