Ben Morley
Ben Morley

Reputation: 33

illegal string body character after dollar sign Shell script from groovy function

I'm trying to perform a shell script from a groovy function loaded by a jenkins-pipeline to retrieve a zip file from an external location. I am building the address out in the function and passing it into the shell script via $. But I am getting a syntax error and I'm not sure why.

I've tried escaping the $ but dont think thats the correct approach here and my code has been coverted from triple single quotes (''') to triple double (""") so I can pass the variable in.

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
    """   
}

the results I get are:

Script1.groovy: 28: illegal string body character after dollar sign; solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}" @ line 28, column 22. curl "${nexusLink}" | grep "" | grep "http" | cut -d'>' -f3 |cut -d'/' -f1 > versionFile.xml

Upvotes: 2

Views: 3828

Answers (1)

user_9090
user_9090

Reputation: 1974

You have to add escape characters like below:-

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

Upvotes: 2

Related Questions