1

I have an array and a function.

Function calls context.sh and executes shell command with variable I want to pass (next from array in this loop).

Target:

  1. Grep file, use item from array every loop
  2. If string is not empty (returns a value from shell script) print line with message and error
  3. Export value 'true' to variable called 'errors found'.

def grepLogs(String) {

    def errorsFound = false

List<String> list = new ArrayList<String>()
list.add("some-exclude")
list.add("anotherone")
list.add("yes")

for (String item : list) {
    System.out.println(item)
    context.sh "errorFinder=$(cat logfile.log | egrep 'error|ERROR'| grep -v ${list()})"
    if (errorFinder) {
        println "Errors in log file " + errorFinder
        errorsFound = true
    }
}

    println "No errors found." 
}

So far I cannot manage to make it check every item from array and change the value. How do I implement this?

1 Answer 1

3

guess you just want to exclude lines with some words from result.

just convert the list into string separated with | (pipe).

so the shell command will look like this:

cat logfile.log | grep 'error|ERROR'| grep -v 'some-exclude|anotherone|yes'

and catch stdout into groovy variable using returnStdout parameter

so the sh call should look like:

def list = [ "some-exclude", "anotherone", "yes" ]
def cmd = "cat logfile.log | grep 'error|ERROR'| grep -v '${ list.join('|') }'"
def errors = sh( returnStdout: true, script: cmd )
if( errors.trim() ){
    println "errors found: ${errors}"
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hey daggett, your answer helped me a lot in solving this problem. Now it works correctly, just tuned it a bit. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.