简体   繁体   English

在jenkins配置外壳中为双引号表达式添加了额外的单引号

[英]Extra single quotes added for a double quote expression in jenkins configuration shell

I am new to bash script. 我是bash脚本的新手。 I am running a bash script. 我正在运行bash脚本。 I want that the expression to be evaluated as "value". 我希望该表达式被评估为“值”。 But when I execute that in jenkins, I am getting '"value"'. 但是当我在詹金斯身上执行它时,我得到的是“价值”。 Not sure if this is something to do with jenkins or shell script. 不知道这是否与jenkins或shell脚本有关。 Below is the sample code : 下面是示例代码:

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

the value I am getting surrounded by single quote. 我被单引号包围的值。 But I have to only the double quoted value. 但是我只需要双引号。 Can you help me to fix/identify issue. 您能帮我解决/确定问题吗?


Editing to show the original script 编辑以显示原始脚本

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

if you see the last but one line.. the expression is giving single quote and then double quotes, that is my problem. 如果看到最后一行,则表达式是单引号然后是双引号,这就是我的问题。 During run time,the same argument is being passed to another script where it is getting single quote which inturn is failing. 在运行时,相同的参数将传递到另一个脚本,在该脚本中单引号又将失败。 However, the echo output is as expected which has only double quotes, but run time output has extra single quotes. 但是,回显输出与预期的一样,只有双引号,但是运行时输出具有额外的单引号。 Hope I am clear this time. 希望这次我清楚。 I could not post actual code as it is compliance related issue. 我无法发布实际代码,因为这是合规性相关的问题。

Your question is a bit unclear to me. 您的问题对我来说还不清楚。

You store the date into a Bash shell variable. 您将日期存储到Bash shell变量中。 You want to pass this variable to another program (I'm guessing that's what you mean when you say 'execute that in Jenkins') but the value of the variable has an extra set of quotes, specifically outer single quotes, which you do not want. 您想将此变量传递给另一个程序(我猜这就是您说“在Jenkins中执行”时的意思),但是变量的值具有额外的引号集,特别是外部单引号,您不会想。 I'm guessing you are asking how to prevent the outer single quotes. 我猜你在问如何防止外部单引号。

Did I understand you correctly? 我对你的理解正确吗?

Does the following similar code help? 以下类似代码是否有帮助?

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

I see your edited question and its honestly still unclear to me. 我看到您编辑过的问题,老实说,我仍然不清楚。 I'm guessing you want to save a variable that has embedded spaces as a complete string in order to pass to another script which needs to process that string. 我猜想您想将具有嵌入空格的变量保存为完整的字符串,以便传递给另一个需要处理该字符串的脚本。 Hopefully this is accurate. 希望这是准确的。

Try this: 尝试这个:

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

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

Note I'm using [] as delimiters only to avoid confusion with quotes; 注意我使用[]作为分隔符只是为了避免与引号引起混淆; I could have used any other character as a delimiter. 我本可以使用其他任何字符作为分隔符。 Now the $mydate variable contains double quotes. 现在,$ mydate变量包含双引号。

Based on your clarifying comment, here's the code you can try: 根据您的注释,可以尝试以下代码:

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

In this code, expr will contain date is "2018-06-21" , where the date value is in double-quotes as you expect. 在此代码中, expr将包含date is "2018-06-21" ,其中日期值如您date is "2018-06-21"用双引号引起来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM