日期命令Linux中的无效日期错误 [英] invalid date error in the date command linux
问题描述
在脚本中,我正在使用命令
In the script I am using the command
CURRENT_DATE_tmp=`date -d $CURRENT_DATE +%Y%m%d`.
它给出错误 date:无效日期`+%Y%m%d'
可能是什么原因.我知道变量CURRENT_DATE中有值.
It gives error date: invalid date `+%Y%m%d'
what may be the reason. I know that the variable CURRENT_DATE have value in it.
推荐答案
date -d $ CURRENT_DATE
将打印与 $ CURRENT_DATE
变量相对应的日期.
date -d $CURRENT_DATE
will print the date corresponding to the $CURRENT_DATE
variable.
$) CURRENT_DATE="20140220"
$) date -d $CURRENT_DATE
Thu Feb 20 00:00:00 IST 2014
要将日期存储到变量中,请尝试使用
To store the date into a variable, try using
$) CURRENT_DATE_TMP=`date +%Y%m%d`
$) echo $CURRENT_DATE_TMP
20140704
编辑
要将现有日期打印为新格式,请使用
To print an existing date into a new format, use
$ CURRENT_DATE=`date +%Y-%m-%d`
$ echo $CURRENT_DATE
2014-07-04
$ date -d$CURRENT_DATE "+%Y%m%d"
20140704
最好还是将 $ CURRENT_DATE
变量用引号引起来,以便带空格的日期不会破坏任何内容.
Better still, wrap the $CURRENT_DATE
variable within quotes, so that dates with spaces don't break anything.
$ CURRENT_DATE=`date`
$ echo $CURRENT_DATE
Fri Jul 4 17:59:45 IST 2014
$ date -d"$CURRENT_DATE" "+%Y%m%d"
20140704
$ date -d$CURRENT_DATE "+%Y%m%d"
date: extra operand ‘4’
在当前示例中,在 -d
标志后有一个空格,请将其删除.
In your current example, you have a space after the -d
flag, remove it.
这篇关于日期命令Linux中的无效日期错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!