【问题标题】:Extra single quotes added for a double quote expression in jenkins configuration shell在 jenkins 配置 shell 中为双引号表达式添加了额外的单引号
【发布时间】:2018-11-30 03:34:11
【问题描述】:

我是 bash 脚本的新手。我正在运行一个 bash 脚本。我希望表达式被评估为“值”。但是当我在詹金斯执行它时,我得到了“价值”。不确定这是否与 jenkins 或 shell 脚本有关。 下面是示例代码:

#!/bin/bash
set -x
Date1=${date}
dql="\"" #double quote literal
Date1=$dql$Date1$dql 
echo $Date1 #"2018-06-21"
expr=$( date is $Date1) # output is ´"2018-06-21"´

我被单引号包围的值。但我必须只有双引号值。你能帮我解决/识别问题吗?


编辑以显示原始脚本

#!/bin/bash

set -x

mydate=`date`              # Save the output of date command into the variable mydate

dquote="\""                # Save the dbl-quote character into the variable dquote

mydate=""${dquote}""${mydate}""${dquote}""     # Construct a string that encases the date with dbl-quotes



eval myexp=$mydate



echo $mydate

echo date is "${mydate}"

#echo -e "\042"



output :





++ date

+ mydate='Sun Jun 24 10:04:12 UTC 2018'

+ dquote='"'

+ mydate='"Sun Jun 24 10:04:12 UTC 2018"'

+ eval 'myexp="Sun' Jun 24 10:04:12 UTC '2018"'

++ myexp='Sun Jun 24 10:04:12 UTC 2018'

+ echo '"Sun' Jun 24 10:04:12 UTC '2018"'

"Sun Jun 24 10:04:12 UTC 2018"

+ echo date is '"Sun Jun 24 10:04:12 UTC 2018"'

date is "Sun Jun 24 10:04:12 UTC 2018"

如果您看到最后一行.. 表达式先给出单引号,然后给出双引号,这就是我的问题。在运行时,相同的参数被传递给另一个脚本,它得到单引号,而单引号又失败了。但是,回显输出与预期的一样,只有双引号,但运行时输出有额外的单引号。希望这次我清楚。我无法发布实际代码,因为它是与合规性相关的问题。

【问题讨论】:

  • 有什么特别的原因将Date1 括在双引号中吗?
  • 对不起,我应该更清楚我的问题。我的问题不在于日期。实际上 expr 除了日期之外还有其他值。这个 expr 将作为参数传递给其他脚本。它期望的格式类似于 [date is "2018-06-21" ........]。我的问题是如果我有值说 Date1 (它可以是任何类型),我想要在评估表达式后对该值加双引号。

标签: bash shell unix jenkins sh


【解决方案1】:

根据您的澄清评论,您可以尝试以下代码:

$ date="2018-06-21"
$ expr="date is \"${date}\""
$ echo $expr
date is "2018-06-21"

在此代码中,expr 将包含 date is "2018-06-21",其中日期值如您所愿使用双引号。

【讨论】:

  • 谢谢。我试过这个。 $ set -x $ date="2018-06-21" + date=2018-06-21 $ expr="date is \"${date}\"" + expr='date is "2018-06-21" ' $ expr1="${expr}" + expr1='date is "2018-06-21"' 如果您在上面看到, expr 和 expr1 都有单引号括起来的值。我在这里不需要单引号。当我执行实际脚本时,我得到单引号值。我在这里错过了什么吗?
  • @user12 这不是你想要的吗?
  • 我用几乎真实的脚本编辑并发布了这个问题。你可以看看,看看你能不能帮助我?
【解决方案2】:

你的问题我有点不清楚。

您将日期存储到 Bash shell 变量中。您想将此变量传递给另一个程序(我猜这就是您说“在 Jenkins 中执行”时的意思)但是变量的值有一组额外的引号,特别是外部单引号,而您没有想。我猜你在问如何防止外部单引号。

我理解正确吗?

以下类似的代码有帮助吗?

#!/bin/bash
mydate=`date`              # Save the output of date command into the variable mydate
dquote="\""                # Save the dbl-quote character into the variable dquote
mydate="${dquote}${mydate}${dquote}"     # Construct a string that encases the date with dbl-quotes 
echo date is [$mydate]     # Output is:  date is ["Wed Jun 20 22:52:46 EDT 2018"]

我看到您编辑的问题,老实说,我仍然不清楚。我猜你想将一个嵌入空格的变量保存为一个完整的字符串,以便传递给另一个需要处理该字符串的脚本。希望这是准确的。

试试这个:

#!/bin/bash
mydate="\"`date`\""
echo date is [$mydate]

% date is ["Mon Jun 25 20:38:00 EDT 2018"]

请注意,我使用 [] 作为分隔符只是为了避免与引号混淆;我可以使用任何其他字符作为分隔符。现在 $mydate 变量包含双引号。

【讨论】:

  • 我应该写得更清楚。我在问题下方添加了评论。在计算表达式后,我希望双引号指向一个值。
  • 我还不清楚。您的句子“我希望在评估表达式后将双引号变为一个值”对我来说有点模棱两可。您是否在问如何将一个 shell 脚本中的值传递给另一个程序并确保双引号也被传递?
  • 我已经编辑了发布几乎我所面临的实际代码问题的问题。希望这次我清楚
猜你喜欢
  • 1970-01-01
  • 2011-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-08
  • 2014-10-04
  • 1970-01-01
相关资源
最近更新 更多