Bash - 日期命令和空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27337757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:57:17  来源:igfitidea点击:

Bash - Date command and space

linuxbashdate

提问by VGe0rge

I am trying to create a script that uses the date command in bash. I am familiar with the basic syntax of the date command. Here is the simple script:

我正在尝试创建一个在 bash 中使用 date 命令的脚本。我熟悉 date 命令的基本语法。这是简单的脚本:

#!/bin/bash 
set -x 
DATE_COMMAND="date "+%y-%m-%d %H:%M:%S"" 
echo "$($DATE_COMMAND)" 
set +x

The thing is that the above code doesn't work. Here is the output:

问题是上面的代码不起作用。这是输出:

+ DATE_COMMAND='date +%y-%m-%d'
+ %H:%M:%S
onlyDate: line 3: fg: no job control
+ echo ''

+ set +x

Ok, so the problem is that the bash splits the command because of the space. I can understand that but I don't know how to avoid that. I have tried to avoid the space with \, to avoid the space and the ". Also the single quotes doesn't seem to work.

好的,所以问题是 bash 由于空间而拆分命令。我可以理解,但我不知道如何避免这种情况。我试图避免空间与\, 避免空间和". 单引号似乎也不起作用。

Please note that I know that this script can be written this way:

请注意,我知道这个脚本可以这样写:

#!/bin/bash
set -x
DATE_COMMAND=$(date "+%y-%m-%d %H:%M:%S")

echo "$DATE_COMMAND"
set +x

I have tried that but I can't use this approach because I want to run the command several times in my script.

我已经尝试过,但我不能使用这种方法,因为我想在我的脚本中多次运行该命令。

Any help will be really appreciated!

任何帮助将不胜感激!

回答by 5gon12eder

The correct approach is to define your own function inside your Bash script.

正确的方法是在 Bash 脚本中定义自己的函数。

function my_date {
  date "+%y-%m-%d %H:%M:%S"
}

Now you can use my_dateas if it were an external program.

现在您可以my_date像使用外部程序一样使用它。

For example:

例如:

echo "It is now $(my_date)."

Or simply:

或者干脆:

my_date

Why isn't your approach working?

为什么你的方法不起作用?

The first problem is that your assignment is broken.

第一个问题是你的任务被破坏了。

DATE_COMMAND="date "+%y-%m-%d %H:%M:%S""

This is parsed as an assignment of the string date +%y-%m-%dto the variable DATE_COMMAND. After the blank, the shell starts interpreting the remaining symbols in ways you did not intend.

这被解析为将字符串分配date +%y-%m-%d给变量DATE_COMMAND。在空白之后,shell 开始以您不希望的方式解释剩余的符号。

This could be partially fixed by changing the quotation.

这可以通过更改报价部分修复。

DATE_COMMAND="date '+%y-%m-%d %H:%M:%S'"

However, this doesn't really solve the problem because if we now use

然而,这并不能真正解决问题,因为如果我们现在使用

echo $($DATE_COMMAND)

It will not expand the argument correctly. The dateprogram will see the arguments '+%y-%m-%dand %H:%M:%S'(with quotes) instead of a single string. This could be solved by using evalas in

它不会正确扩展参数。该date程序将看到的参数'+%y-%m-%d%H:%M:%S'(带引号),而不是一个字符串。这可以通过使用来解决eval,如

DATE_COMMAND="date '+%y-%m-%d %H:%M:%S'"
echo $(eval $DATE_COMMAND)

where the variable DATE_COMMANDis first expanded to the string date '+%y-%m-%d %H:%M:%S'that is then evaluated as if it were written like so thus invoking datecorrectly.

其中变量DATE_COMMAND首先被扩展为字符串date '+%y-%m-%d %H:%M:%S',然后eval被使用,就好像它是这样写的,从而date正确调用。

Note that I'm only showing this to explain the issue. evalis not a good solution here. Use a function instead.

请注意,我展示这个只是为了解释这个问题。 eval在这里不是一个好的解决方案。改用函数。

PS It is better to avoid all-uppercase identifier strings as those are often in conflict with environment variables or even have a magic meaning to the shell.

PS 最好避免全大写的标识符字符串,因为它们经常与环境变量冲突,甚至对 shell 具有神奇的意义。

回答by Dani?lGu

Escaping the space works for me.

逃离空间对我有用。

echo `date +%d.%m.%Y\ %R.%S.%3N` 

回答by Thanh Trung

Shortest answer is

最短的答案是

#if you want to store in a variable
now=$(date '+%F" "%T');
echo $now

#or direct output
date '+%F" "%T'

回答by Grats

For long scripts

对于长脚本

declare variables section:

声明变量部分:

dateformat="%Y-%m-%d %H:%M:%S"

anywhere to get date:

获取日期的任何地方:

datestr=`date +"${dateformat}"`

anywhere to echo date:

任何地方回显日期:

echo ${datestr}

For short simple scripts Dani?lGu's answer is the best:

对于简短的简单脚本,Dani?lGu 的回答是最好的:

echo `date +%d.%m.%Y\ %R.%S.%3N`