从Unix Shell脚本获取当前日期 [英] Get Current date in epoch from Unix shell script
问题描述
如何获取当前日期值,即从1970-1-1开始经过的天数。我需要使用Unix Shell脚本解决方案。
How to get the current date value in epoch i.e., number of days elapsed since 1970-1-1. I need solution in unix shell script.
推荐答案
更新:先前在此处发布的答案链接到自定义脚本不再可用,仅是因为OP指出 date +'%s'
对他不起作用。请参阅 UberAlex的答案和 cadrian对于正确的解决方案的答案。简而言之:
Update: The answer previously posted here linked to a custom script that is no longer available, solely because the OP indicated that date +'%s'
didn't work for him. Please see UberAlex' answer and cadrian's answer for proper solutions. In short:
-
对于自Unix时代以来的秒数,请使用
date(1)
如下:
For the number of seconds since the Unix epoch use
date(1)
as follows:
date +'%s'
对于自Unix时代以来的天数,将结果除以一天中的秒数(注意双括号!):
For the number of days since the Unix epoch divide the result by the number of seconds in a day (mind the double parentheses!):
echo $(($(date +%s) / 60 / 60 / 24))
这篇关于从Unix Shell脚本获取当前日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!