Bash-日期命令和空格 [英] Bash - Date command and space

查看:193
本文介绍了Bash-日期命令和空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

#!/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

好,所以问题在于bash会因为空间而将命令拆分。我能理解,但我不知道如何避免。我试图用 \ 避免空格,避免使用空格和 。而且单引号也没有

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.

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

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.

任何帮助将不胜感激!

推荐答案

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

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

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

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

Now you can use my_date as if it were an external program.

例如:

echo "It is now $(my_date)."

或者简单地:

my_date

为什么您的方法行不通?

第一个问题是您的作业被打破了。

The first problem is that your assignment is broken.

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

这被解析为字符串 date +%y-%m- %d 到变量 DATE_COMMAND 。空格后,shell开始以您不希望使用的方式解释其余符号。

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

可以通过更改引号来部分解决此问题。

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)

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

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

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

其中变量 DATE_COMMAND 首先扩展为字符串 date&'n%+%y-%m-% d%H:%M:%S'然后将 eval 赋值,就像这样写,因此调用日期正确。

where the variable DATE_COMMAND is 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 date correctly.

请注意,我仅显示此内容以解释问题。 eval 在这里不是一个好的解决方案。

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

PS最好避免使用全大写的标识符字符串,因为这些字符串通常与环境变量冲突,甚至对shell都具有神奇的含义。

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.

这篇关于Bash-日期命令和空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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