title: Execute stage when shell command returns empty string
body: <p>I'm trying to create a pipeline that executes a series of steps only if a shell command has returned an empty string.
  Here is the whole pipeline, with the relevant section being stage "Start container":</p>
  
  <pre><code>pipeline {
    agent {
      docker {
        image 'maven:3-alpine'
        args '-v m2:/root/.m2'
      }
    }
    stages {
      stage('Build') {
        steps {
          sh 'mvn verify -DskipTests'
          archiveArtifacts(artifacts: 'target/*.war', fingerprint: true, onlyIfSuccessful: true)
        }
      }
      stage('Test') {
        post {
          always {
            junit 'target/surefire-reports/**/*.xml'
  
          }
        }
        steps {
          sh 'mvn test'
        }
      }
      stage('Deploy [dev]') {
        environment {
          CONTAINER = 'containerName'
          STARTUP_LOG = 'INFO: Server startup'
        }
        stages {
          stage('Install docker') {
            steps  {
              sh 'apk update &amp;&amp; apk add docker'
            }
          }
          stage('Start container') {
            when {
              expression {
                sh '''if [ -z $(docker ps -f name=$CONTAINER -q) ]; then true; else false; fi'''
              }
            }
            steps {
              sh '''docker start $CONTAINER'''
              sh '''(docker logs -f --tail 10 $CONTAINER 2&gt;&amp;1 &amp;) | grep -q "$STARTUP_LOG"'''
            }
          }
          stage('Deploy WAR') {
            steps {
              echo 'deploy war file'
              echo 'smoke test'
            }
          }
        }
      }
      stage('Acceptance') {
        steps {
          echo 'deploy stubs'
          echo 'run cucumber tests'
          echo 'publish report'
        }
      }
      stage('Release') {
        when {
          expression {
            env.BRANCH_NAME == 'master'
          }
  
        }
        steps {
          echo 'releasing'
          echo 'time to deploy to UAT'
        }
      }
    }
  }
  </code></pre>
  
  <p>The problem is I'm getting a red flag during the execution of the when expression:</p>
  
  <blockquote>
  <pre><code>[test_master-YKUBYJHFWV5SAO5BKANKYXEX6RUGQUBB2GGZLHYPHNUKZELZPATQ] Running shell script
  
  + docker ps -f name=containerName -q
  + [ -z c504c975f632 ]
  + false
  script returned exit code 1
  </code></pre>
  </blockquote>
  
  <p>I thought if the expression returned a false or a null then the associated stage was not going to be executed, but apparently, my understanding is wrong...</p>
  
  <p>Any advice on how this can be achieved? Possibly without recurring to some magic goo :-D</p>
  
  <h2>UPDATE</h2>
  
  <p>I have managed to fix the build error by adding the parameter <code>returnStatus:true</code> to the <code>sh</code> command, but it still doesn't prevent the execution of the step.</p>
  
  <p>I've replaced the "Start container" stage now looks like this:</p>
  
  <pre><code>    stage('Start container') {
        when {
          expression {
            sh script: '''if [ -z $(docker ps -f name=$CONTAINER -q) ]; then true; else false; fi''', returnStatus: true
          }
        }
        steps {
          sh '''docker start $CONTAINER'''
          sh '''(docker logs -f --tail 10 $CONTAINER 2&gt;&amp;1 &amp;) | grep -q "$STARTUP_LOG"'''
        }
      }
  </code></pre>