从Unix Shell脚本获取当前日期 [英] Get Current date in epoch from Unix shell script

查看:439
本文介绍了从Unix Shell脚本获取当前日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取当前日期值,即从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:


  1. 对于自Unix时代以来的秒数,请使用 date(1) 如下:

  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屋!

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