Home > Mobile >  Jenkins script how to compare two string variables
Jenkins script how to compare two string variables

Time:07-18

  1. def pname = "netstat -ntlp|grep 8080|awk '{printf \$7}'|cut -d/ -f2"

  2. sh "echo $pname" \ java

  3. if ("java".equals(pname)) { sh "echo 1111" }

The process corresponding to port 8080 is a java process, and the 2nd line print "java". But the body of the if statement just doesn't execute.

CodePudding user response:

You seem to be not executing the command correctly. Please refer to the following sample. Please note the returnStdout: true to return output of the command.

pipeline {
    agent any
    stages {
        stage('Test') {
            steps {
                script {
                    def pname = sh(returnStdout: true, script: "netstat -ntlp|grep 8080|awk '{printf \$7}'|cut -d/ -f2").trim()
                    if (pname == "java") { 
                        echo "echo 1111" 
                    }
                }
            }
        }
    }
}

CodePudding user response:

try "==" for equal or you can read doc. https://groovy-lang.org/operators.html#_relational_operators

  • Related