【问题标题】:illegal string body character after dollar sign Shell script from groovy function来自groovy函数的美元符号Shell脚本后的非法字符串正文字符
【发布时间】:2019-06-07 09:29:00
【问题描述】:

我正在尝试从由 jenkins 管道加载的 groovy 函数执行 shell 脚本,以从外部位置检索 zip 文件。我在函数中构建地址并通过 $ 将其传递到 shell 脚本中。但我收到语法错误,我不知道为什么。

我尝试转义 $ 但不认为这是正确的方法,并且我的代码已从三重单引号 (''') 转换为三重双引号 ("""),因此我可以将变量传入。

def DownloadBaseLineFromNexus(groupID, artifactID){
    //add code for this method
    def nexusLink = "${GetNexusLink()}/${GetNexusProdRepo()}/${groupID}/${artifactID}/"

    sh """
    # retrieving all available version  from release repo to versionFile.xml
    curl ${nexusLink} | grep "<a href=.*</a>" | grep "http" | cut -d'>' -f3 |cut -d'/' -f1 > versionFile.xml
    # creating array from versionFile.xml
    fileItemString=$(cat versionFile.xml |tr "\n" " ")
    fileItemArray=($fileItemString)
    # Finding maximum of array element
    maxValue=`printf "%d\n" "${fileItemArray[@]}" | sort -rn | head -1`
    # Download latest version artifact from nexus
    curl -o ${(artifactID)}.zip ${(nexusLink)}/${(artifactID)}-$maxValue.zip
    # Unzip the tool
    unzip ${(artifactID)}.zip
    """   
}

我得到的结果是:

Script1.groovy: 28: 美元符号后面的非法字符串主体字符; 解决方案:要么转义文字美元符号 "\$5",要么将值表达式 "${5}" @ 第 28 行第 22 列括起来。 curl "${nexusLink}" | grep "" | grep "http" | cut -d'>' -f3 |cut -d'/' -f1 > versionFile.xml

【问题讨论】:

  • 您有 $ 符号,后面没有变量/表达式。例如:fileItemString=$(cat versionFile.xml |tr "\n" " ")。这个$(cat ...你必须像\$(cat ...一样逃跑

标签: jenkins groovy jenkins-pipeline


【解决方案1】:

您必须添加如下转义字符:-

curl ${nexusLink} | grep \"<a href=.*</a>\" | grep \"http\" | cut -d'>' -f3 |cut -d'/' -f1 > versionFile.xml

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-19
    • 1970-01-01
    • 2015-08-21
    • 2019-08-22
    • 1970-01-01
    • 2012-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多