In a Jenkins pipeline, I am trying to retrieve the output of a shell script into a variable.
Example
list_test = sh(script:'ls | grep test', returnStdout:true).trim() list_test.each { test -> method(test) } This works fine as long as there is indeed an output. The problem is when the output of 'ls | grep test' is empty.
I end up with an error.
So far, the only workaround I found was:
try { list_test = sh(script:'ls | grep test', returnStdout:true).trim() } catch (Exception ex) { println("output is empty") } if (list_test) { list_test.each { test -> method(test) } } Would there be any better way to handle that kind of issue ?