<p>I'm trying to create a Jenkins pipeline with multi-stages, the first stage creates the image, the second stage create new pods from the newly created image and the third stage checks if each pods is alive.</p>
<p>In the third stage one of the commands is:</p>
<p><code>result = sh(returnStdout: true, script: &quot;kubectl get pods --all-namespaces | grep -i &lt;specific pod name&gt; | wc -l&quot;).trim()</code></p>
<p>and the value I expect to receive is 1 or 0 but when the command return 0 there is an unexpected EOF error:</p>
<p>/var/jenkins_home/workspace/git-test@tmp/durable-c72ab15c/script.sh: line 1: unexpected EOF while looking for matching `''</p>
<p>after the run failed the location from the error doesn't exist.</p>
<p>How can I get the value I need?</p>
<p>The Stage looks like this:</p>
<pre><code>  def isDone = false
  def response = &quot;&quot;
  for (int i = 0; i &lt; 10 &amp;&amp; isDone == false; i++) {
    timeout(time: 60, unit: 'SECONDS') {
      waitUntil {
        response = sh(returnStdout: true, script: &quot;kubectl get pods -o=name --all-namespaces --field-selector status.phase=Running | grep -i $testCaseName | grep . |  awk '{ print (\$1 == \&quot;\&quot;) ? \&quot;0\&quot; : \&quot;1\&quot; }'&quot;).trim()
        if (response != &quot;&quot;) {
          return true
        }
      }
    }
    if (response == '1') {
      sh &quot;echo '$testCaseName pod is running'&quot;
      isDone = true
    } else {
      sh &quot;echo '$testCaseName pod isn't running yet'&quot;
      sleep(time:1,unit:&quot;MINUTES&quot;)
    }
</code></pre>

RandomGuy17
<p>I still don't know for sure the right cause for this problem but I'm assuming that it came from the <code>readStdout</code> flag from the Jenkins <code>sh</code> command.</p>
<p>The solution for this problem is to wrap with <code>try catch</code> the command that causes the <code>unexpected EOF</code> error which here is the <code>if</code> section and put the else section in the <code>catch</code> section. it looks like this:</p>
<pre><code>def isDone = false
def response = &quot;&quot;
def state = &quot;&quot;
sh &quot;mkdir -p ${workspace}/results/${currentBuildNumber}/$testCaseName&quot;
for (int i = 0; i &lt; 10 &amp;&amp; isDone == false; i++) {
  sh &quot;kubectl get pods --all-namespaces | grep -i $testCaseName | awk '{ printf (\$4 == \&quot;Running\&quot;) ? \&quot;yes\&quot; : \&quot;'wait'\&quot; }' &gt; ${workspace}/results/${currentBuildNumber}/$testCaseName/commandResult.txt&quot;
  sh &quot;kubectl get pods --all-namespaces | grep -i $testCaseName | awk '{ printf \$4 }' &gt; ${workspace}/results/${currentBuildNumber}/$testCaseName/podState.txt&quot;
  timeout(time:30,unit:&quot;SECONDS&quot;) {
    waitUntil {
      response = readFile &quot;${workspace}/results/${currentBuildNumber}/$testCaseName/commandResult.txt&quot;
      return (response != &quot;&quot;)
    }
  }
  timeout(time:30,unit:&quot;SECONDS&quot;) {
    waitUntil {
      state = readFile &quot;${workspace}/results/${currentBuildNumber}/$testCaseName/podState.txt&quot;
      return (state != &quot;&quot;)
    }
  }
  try {
    if (&quot;$response&quot; == &quot;yes&quot;) {
      println(&quot;$testCaseName pod is running&quot;)
      isDone = true
    } else {
      println(&quot;$testCaseName pod state is: $state&quot;)
      sleep(time:1,unit:&quot;MINUTES&quot;)
    }
  } catch(Exception e) {
    println(&quot;$testCaseName pod state is: $state&quot;)
    if ((i == 9) &amp;&amp; (isDone == true)) {
      error(&quot;Build failed because $testCaseName pod couldn't start&quot;)
    } else {
      sleep(time:1,unit:&quot;MINUTES&quot;)
    }
  }
}
</code></pre>
