如何在 OS X Leopard 上的 bash 脚本中进行日期数学运算?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/498358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I do date math in a bash script on OS X Leopard?
提问by Gregory Higley
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.
我意识到我可以编写一个小 C 或 Ruby 程序来执行此操作,但我希望我的脚本具有尽可能少的依赖项。
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:
鉴于这一警告,如何在 OS X 上的 bash 脚本中进行日期数学运算?我看过一个帖子(在另一个网站上),有人做了以下事情:
date -d "-1 day"
But this does not seem to work on OS X.
但这似乎不适用于 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.
一些人评论并回应说 Ruby、Python、Perl 等是 OS X 的标准配置。我熟悉所有这三种语言,可以轻松编写满足我要求的脚本。事实上,我已经有了这样一个用 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.
所以也许我应该澄清一下我所说的“外部依赖”是什么意思。我的意思是,我不希望我的 bash 脚本必须调用它外部的任何其他脚本。换句话说,我希望它使用一些在 OS X 的 vanilla 安装中可用并且已经在路径上的实用程序。
However, it doesn't look like this is possible, so I will have to make due with my external dependency: a Ruby script.
但是,这看起来不太可能,所以我将不得不考虑我的外部依赖:一个 Ruby 脚本。
回答by Matthew Schinckel
$ date -v -1d
-d sets Daylight Savings time flag.
-d 设置夏令时标志。
Try man date for more info.
尝试 man date 以获取更多信息。
回答by James Toher
For example, this OS X date command will add 14 days to the input string 20160826 (not the present time):
例如,此 OS X 日期命令将向输入字符串 20160826(不是当前时间)添加 14 天:
date -j -f %Y%m%d -v+14d 20160826 +%Y%m%d
回答by Jeff Wu
Because MacOS is based on BSD, not linux, the command is actually:
因为MacOS是基于BSD的,不是linux,所以命令其实是:
date -v-1d
The adjust switch takes the format -v[+/-](number)[ymwdHMS].
调整开关采用格式-v[+/-](number)[ymwdHMS]。
回答by Arthur Reutenauer
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.
马修提到的命令对我适用于 Leopard 上的 /bin/date。此外,正如 Schwern 指出的那样,如果您确定您的 shell 脚本将在 Mac OS X 上运行,为什么不想要使用 Python 或 Ruby,它们都是 /usr/bin 中的标准配置?这绝不会影响您的特定场景中的可移植性。
回答by dbr
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.
我意识到我可以编写一个小 C 或 Ruby 程序来执行此操作,但我希望我的脚本具有尽可能少的依赖项。
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)..
使用 C 或作为 Ruby 脚本执行将导致更少的依赖性,并且更加跨平台.. Bash 脚本只是调用各种其他脚本/应用程序,主要是 GNU 实用程序.. 许多命令不可用,或采用不同的参数在其他平台上(尤其是在 OS X 和 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.
默认情况下,Ruby 包含在 OS X Leopard(以及 10.4,如果我没记错的话,可能还有以前的版本)中,如果您使用 Rubys 内置日期库,它将在任何平台上一致地工作。
Whenever you try to do anything remotely complicated, you are generally best of using a "proper" scripting language!
每当您尝试做任何远程复杂的事情时,通常最好使用“正确的”脚本语言!
回答by Brian Kuepper
Here is a script that I use to print the dates for my journal in google doc's:
这是我用来在 google doc 中打印日志日期的脚本:
#!/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):
这是示例输出,(我在每个日期之间使用 3 个新行):
Sun 4/1/18
周日 4/1/18
Mon 4/2/18
18 年 4 月 2 日星期一
Tue 4/3/18
2018 年 4 月 3 日星期二
Wed 4/4/18
18 年 4 月 4 日星期三
Thu 4/5/18
周四 4/5/18
Fri 4/6/18
周五 4/6/18
Sat 4/7/18
周六 4/7/18
回答by newdark-it
You might be looking for this. gdate '+%s' -d '2 weeks ago'or gdate '+%s' -d '1 day ago'
你可能正在寻找这个。gdate '+%s' -d '2 weeks ago'或者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.
这两个例子在大纪元时间返回。我把那些秒变成了我的语言可以正常阅读的东西strptime(seconds, '%s')。Python Ruby 和 Javascript 是我所知道的少数拥有它的人。我相信 Java 和所有其他人也这样做。
Also gdateis date (GNU coreutils)it just called something different on mac. Strange thing is on my other mac it let me use just datato get this same off set. Don't know why might be they change something recently.
也gdate就是日期(GNU的coreutils) ,它只是叫的东西在Mac上的不同。奇怪的是在我的另一台 Mac 上它让我使用它data来获得相同的偏移量。不知道为什么他们最近可能会改变一些东西。
回答by David Timmons Staskiewicz
I used the following on, macOS Mojave 10.14.5, to find the DHCP renewal time open
我在 macOS Mojave 10.14.5 上使用了以下内容,以找到 DHCP 更新时间打开
date -v +`ipconfig getoption en0 renewal_t1_time_value`S

