【问题标题】:Invalid character in date/time specification日期/时间规范中的无效字符
【发布时间】:2015-02-07 01:11:16
【问题描述】:

示例代码:

modified_time=`ls -lt core* | head -1 | awk '{print $6,$7,$8}'`
echo modified time = $modified_time

我正在尝试借助 aix box 上的以下命令以秒为单位转换文件的最后修改时间

t2=`date +'%s' -d "$modified_time"`
echo t2 = $t2

注意:我发布的代码正在 bash 上的 cygwin 上运行。但是它在 AIX ( ksh ) 上给出错误。 我收到以下错误:

egdev04{stc}[/home/stc]% t2=`date +'%s' -d "$modified_time"`

Invalid character in date/time specification.
Usage: date [-u] [+Field Descriptors]

有人可以告诉我代码的哪一部分是错误的,并建议需要改用什么。

【问题讨论】:

  • 你不应该使用旧的和过时的反引号,使用括号。第一行也可以缩短一些:modified_time=$(ls -lt core* | awk 'NR==1 {print $6,$7,$8}')
  • 根据报错信息,错误的部分是date命令。请尝试date -d "$modified_time" +'%s'
  • 它没有用。我发布的代码正在 bash 上的 cygwin 上运行。但是它在 AIX ( ksh ) 上给出错误。
  • @Ameet 请使用此重要信息更新您的原始帖子。
  • echo modified time = $modified_time的输出是什么

标签: shell unix awk scripting aix


【解决方案1】:

不幸的是,date(1) 并没有被标准所涵盖,尤其是在过时的系统上,例如 AIX(不是双关语)。

即使在现代 GNU/Linux 与 BSD 系统上,也有不同的键来实现您尝试调用的行为:

  • GNU 日期只有一个键:

    -d, --date=STRING
            display time described by STRING, not 'now'
    
  • BSD 日期将使用两个键和特殊调用:

    date [-jnRu] -f input_fmt new_date  [+output_fmt]
    
    -j       Do not try to set the date.  This allows you to use the -f flag
             in addition to the + option to convert one date format to
             another.
    -f       Use input_fmt as the format string to parse the new_date provided
             rather than using the default [[[[[cc]yy]mm]dd]HH]MM[.ss] format.
             Parsing is done using strptime(3).
    

AIX 似乎不包含这些工具中的任何一个。因此,最终,如果您真的需要,您将不得不使用某种脚本语言(例如 Perl/Ruby/Python/等)执行微脚本。

退一步,解析ls(1) 的结果总是一个非常糟糕的主意,因为它们往往会根据特定的操作系统实现、语言环境、输出格式、“人类可读”默认值等而大不相同。如果你真的只是想获得一些文件修改时间,为什么不使用stat(1)?它可能在 AIX 上可用吗?类似的东西

stat -c '%Y' "$file"

似乎解决了你的任务。

【讨论】:

    猜你喜欢
    • 2020-04-14
    • 2011-11-09
    • 2013-04-03
    • 1970-01-01
    • 2011-08-11
    • 1970-01-01
    • 2012-05-18
    • 1970-01-01
    • 2015-07-23
    相关资源
    最近更新 更多