【发布时间】: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