日期:2014-05-16  浏览次数:21089 次

关于linux中date +%s获取绝对秒数异常的问题
我在一个做统计的shell脚本里用date +%s获取当前的绝对秒数,但是运行了一个月左右,这条命令获得的值就变为了空值,第一次出现时以为是个意外,就没在意,重启脚本后就没管了。但是这次是第二次出现这个问题了,实在不清楚是什么原因,请问是否有人遇到过这样的问题,如果知道还希望不吝赐教,谢谢啦~~
linux date 绝对秒数 异常

------解决方案--------------------
这种情况没有看到过啊。
建议在脚本中判断下返回值 $? 是否为0
------解决方案--------------------
建议用 date "+%s"
------解决方案--------------------
建议试试,% 有可能有特殊含义

Quoting

Quoting is used to remove the special meaning of certain characters or words to the shell. Quoting can be used to preserve the literal meaning of the special characters in the next paragraph, prevent reserved words from being recognized as such, and prevent parameter expansion and command substitution within here-document processing (see Here-Document ).

The application shall quote the following characters if they are to represent themselves:


------解决方案--------------------
  &  ;  <  >  (  )  $  `  \  "  '  <space>  <tab>  <newline>

and the following may need to be quoted under certain circumstances. That is, these characters may be special depending on conditions described elsewhere in this volume of POSIX.1-2008:

*   ?   [   #   ?   =   %

The various quoting mechanisms are the escape character, single-quotes, and double-quotes. The here-document represents another form of quoting; see Here-Document .
------解决方案--------------------
从lz描述"统计的shell脚本里用date +%s获取当前的绝对秒数"这里的%s(是大写字母);
------解决方案--------------------
接上,date +%S(大写的S),可以满足lz要求,date --help 查看(%S Second(00..60))