简体   繁体   中英

Execute bash command in jenkinsfile groovy

Help please

Here i have a part of Jenkinsfile like

@Library('groovy_shared_libraries')_

stage("Ensure droplet don`t exist and Create DO Droplet") {
    // Ensure droplet don`t exist
    ExistDroplet = sh(
        script: "doctl compute droplet list | awk '{gsub(/\./, "", $2)} 1' | grep -w $(echo $FullDomainName | sed "s/\.//g") | awk '{ print $2 }' | wc -l"
        returnStdout: true
    ).trim()

How i can execute this bash command in jenkinsfile groovy?

doctl compute droplet list | awk '{gsub(/\./, "", $2)} 1' | grep -w $(echo $FullDomainName | sed "s/\.//g") | awk '{ print $2 }' | wc -l

With the current implementation, it returns an error

WorkflowScript: 26: unexpected char: '\' @ line 26, column 63.
   te droplet list | awk '{gsub(/\./, "", $

if i add additional \ to command i have this error

WorkflowScript: 26: illegal string body character after dollar sign;
   solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}" @ line 26, column 21.
           script: `"doctl compute droplet list | awk '{gsub(/\\./, "", $2)} 1' | grep -w $(echo $FullDomainName | sed "s/\\.//g") | awk '{ print $2 }' | wc -l",`

after adding escape $2 jenkins show this error

WorkflowScript: 26: illegal string body character after dollar sign;
   solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}" @ line 26, column 21.
           script: `"doctl compute droplet list | awk '{gsub(/\\./, "", \$2)} 1' | grep -w $(echo $FullDomainName | sed "s/\\.//g") | awk '{ print \$2 }' | wc -l",`

This all works with this command doctl compute droplet list | grep -w \"$FullDomainName\" | awk '{ print \$2 }' | wc -l doctl compute droplet list | grep -w \"$FullDomainName\" | awk '{ print \$2 }' | wc -l doctl compute droplet list | grep -w \"$FullDomainName\" | awk '{ print \$2 }' | wc -l but i need to add

 awk '{gsub(/\\./, "", $2)} 1' | grep -w $(echo $FullDomainName | sed "s/\\.//g")

\ is the escape character for the shell and Jenkins. If you want to send the backslash character to the shell you need to escape it with another one for Jenkins like so (notice the \\ ):

script: "doctl compute droplet list | awk '{gsub(/\\./, "", $2)} 1' | grep -w $(echo $FullDomainName | sed "s/\\.//g") | awk '{ print $2 }' | wc -l"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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