如何在 OS X Leopard 上的 bash 脚本中进行日期数学运算?

发布于 2024-07-12 22:28:07 字数 495 浏览 6 评论 0原文

我意识到我可以编写一个小的 C 或 Ruby 程序来完成此操作,但我希望我的脚本具有尽可能少的依赖项。

鉴于此警告,如何在 OS X 上的 bash 脚本中进行日期数学运算? 我看过一篇文章(在另一个网站上),其中有人做了以下操作:

date -d "-1 day"

但这似乎不适用于 OS X。

附录:

有几个人评论并回复说 Ruby、Python、Perl、等等都是 OS X 的标准配置。我熟悉这三种语言,并且可以轻松编写一个脚本来完成我想要的任务。 事实上,我已经有了这样一个用 Ruby 编写的脚本。

所以也许我应该澄清一下“外部依赖”的含义。 我的意思是,我不希望我的 bash 脚本必须调用它外部的任何其他脚本。 换句话说,我希望它使用 OS X 普通安装中可用且已在路径中的某些实用程序。

然而,这看起来不可能,所以我必须利用我的外部依赖项:Ruby 脚本。

I realize I could whip up a little C or Ruby program to do this, but I want my script to have as few dependencies as possible.

Given that caveat, how does one do date math in a bash script on OS X? I've seen a post (on another site) where someone did the following:

date -d "-1 day"

But this does not seem to work on OS X.

Addendum:

Several people have commented and responded that Ruby, Python, Perl, and the like come standard with OS X. I'm familiar with all three of these languages and could easily write a script that does what I want. As a matter of fact, I already have such a script, written in Ruby.

So perhaps I should clarify what I mean by 'external dependency'. What I mean is, I don't want my bash script to have to call any other script external to it. In other words, I want it to use some utility available in a vanilla installation of OS X and already on the path.

However, it doesn't look like this is possible, so I will have to make due with my external dependency: a Ruby script.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

莫相离 2024-07-19 22:28:07
$ date -v -1d

-d 设置夏令时标志。

尝试男人约会以获取更多信息。

$ date -v -1d

-d sets Daylight Savings time flag.

Try man date for more info.

知足的幸福 2024-07-19 22:28:07

例如,此 OS X 日期命令会将 14 天添加到输入字符串 20160826(不是当前时间):

date -j -f %Y%m%d -v+14d 20160826 +%Y%m%d

For example, this OS X date command will add 14 days to the input string 20160826 (not the present time):

date -j -f %Y%m%d -v+14d 20160826 +%Y%m%d
屌丝范 2024-07-19 22:28:07

因为 MacOS 基于 BSD,而不是 Linux,所以命令实际上是:

date -v-1d

调整开关采用格式 -v[+/-](number)[ymwdHMS]

Because MacOS is based on BSD, not linux, the command is actually:

date -v-1d

The adjust switch takes the format -v[+/-](number)[ymwdHMS].

嗳卜坏 2024-07-19 22:28:07

Matthew 提到的命令对我来说适用于 Leopard 上的 /bin/date 。 此外,正如 Schwern 指出的那样,如果您确定您的 shell 脚本将在 Mac OS X 上运行,为什么不想使用 Python 或 Ruby,它们都是 /usr/bin 中的标准配置呢? 这绝不会影响您特定场景中的可移植性。

The command Matthew mentions works for me with /bin/date on Leopard. Besides, as Schwern points out, if you're sure your shell script is going to be run on Mac OS X, why don't you want to use Python or Ruby, that both come in standard in /usr/bin? This would in no way affect portability in your particular scenario.

dawn曙光 2024-07-19 22:28:07

我意识到我可以编写一个小的 C 或 Ruby 程序来完成此操作,但我希望我的脚本具有尽可能少的依赖项。

用 C 或 Ruby 脚本执行会减少依赖性,并且更具跨平台性。Bash 脚本只是调用各种其他脚本/应用程序,主要是 GNU 实用程序。许多命令不可用,或者采用不同的参数在其他平台上(特别是在 OS X 和 Linux 之间)..

OS X Leopard (和 10.4,如果我没记错的话,可能还有以前的版本)默认包含 Ruby,如果您使用 Ruby 的内置日期库,它将可以工作在任何平台上一致。

每当您尝试做任何远程复杂的事情时,通常最好使用“正确的”脚本语言!

I realize I could whip up a little C or Ruby program to do this, but I want my script to have as few dependencies as possible.

Doing in C or as a Ruby script would result in less dependancies, and be more cross-platform.. Bash-scripting just calls various other scripts/applications, mainly the GNU utilities.. Many of the commands are unavailable, or take different arguments on other platforms (especially between OS X and Linux)..

Ruby is included by default on OS X Leopard (and 10.4, and probably previous versions if I recall correctly), and if you use Rubys built-in date libraries, it will work consistently on any platform.

Whenever you try to do anything remotely complicated, you are generally best of using a "proper" scripting language!

随波逐流 2024-07-19 22:28:07

这是一个脚本,我用它在谷歌文档中打印我的日记的日期:

    #!/bin/bash

    # ask user for variable details
    read -p 'Starting date in "mm-dd-yyyy" format: ' DATE

    # adjust the count by editing the "x" number in {0..x}
    # 91 days will give you about 3 months, currently set to 6 for testing
    # %a gives short day of the week, -m -d gives single digit months and days, capital Y gives 4 digit year

    for i in {0..6}
    do
         NEXT_DATE=$(date -j -f %m-%d-%Y -v+"$i"d $DATE +'%a %-m/%-d/%y')
         echo -e "$NEXT_DATE\n\n\n"
    done

这是示例输出,(我在每个日期之间使用 3 个新行):

Sun 4/1/18

Mon 4/2/18

Tue 4/ 3/18

星期三 4/4/18

星期四 4/5/18

星期五 4/6/18

星期六 4/7/18

Here is a script that I use to print the dates for my journal in google doc's:

    #!/bin/bash

    # ask user for variable details
    read -p 'Starting date in "mm-dd-yyyy" format: ' DATE

    # adjust the count by editing the "x" number in {0..x}
    # 91 days will give you about 3 months, currently set to 6 for testing
    # %a gives short day of the week, -m -d gives single digit months and days, capital Y gives 4 digit year

    for i in {0..6}
    do
         NEXT_DATE=$(date -j -f %m-%d-%Y -v+"$i"d $DATE +'%a %-m/%-d/%y')
         echo -e "$NEXT_DATE\n\n\n"
    done

Here is the sample output, (I use 3 new lines between each date):

Sun 4/1/18

Mon 4/2/18

Tue 4/3/18

Wed 4/4/18

Thu 4/5/18

Fri 4/6/18

Sat 4/7/18

梦回旧景 2024-07-19 22:28:07

您可能正在寻找这个。 gdate '+%s' -d '2 周前'gdate '+%s' -d '1 天前'

这两个示例以 Epoch 时间返回。 我将这些秒转换为我的语言可以正常读取的 strptime(seconds, '%s')。 据我所知,Python、Ruby 和 Javascript 是少数拥有此功能的语言。 我确信 Java 和所有其他语言也是如此。

另外,gdatedate (GNU coreutils),它只是在 mac 上调用了不同的东西。 奇怪的是,在我的另一台 Mac 上,它允许我仅使用 data 来获得相同的偏移量。 不知道为什么他们最近改变了一些东西。

You might be looking for this. gdate '+%s' -d '2 weeks ago' or gdate '+%s' -d '1 day ago'

These two examples return in the Epoch time. I turn those seconds in to something my language can read normal strptime(seconds, '%s'). Python Ruby and Javascript are the few I know have it. I am sure Java and all the others do as well.

Also gdate is date (GNU coreutils) it just called something different on mac. Strange thing is on my other mac it let me use just data to get this same off set. Don't know why might be they change something recently.

冷了相思 2024-07-19 22:28:07

我在 macOS Mojave 10.14.5 上使用以下命令来查找 DHCP 续订时间是否打开

date -v +`ipconfig getoption en0 renewal_t1_time_value`S

I used the following on, macOS Mojave 10.14.5, to find the DHCP renewal time open

date -v +`ipconfig getoption en0 renewal_t1_time_value`S
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文