expect - how to discard the buffer if expected string is not found

StackOverflow https://stackoverflow.com/questions/21824281

  •  12-10-2022
  •  | 
  •  

سؤال

I need to spawn a script that produces lots of output which makes the regex matching of the output slow. Also the buffer fills quickly even when I use quite a large match_max value.

I would like to check the output for a particular string. If the string does not exist, I would like to discard the output read so far.

I have tried using default matches, globs and negative regexes to catch the unwanted strings, but could not get this working.

How can this be done with expect?

هل كانت مفيدة؟

المحلول

This 'seems' to work (more testing is required):

set success_string "\[INFO\] Started Jetty\r\n"

spawn "/usr/bin/mvn" "-pl" ":cloud-client-ui" "jetty:run"
expect {
  -re "(\[^\r]*\)\r\n" 
  {
    set current_line $expect_out(buffer)
    if { [string equal "$current_line" "$success_string"] } {
       puts "exiting with matched string $current_line"
       exit 0
    } else { 
       puts "discarding $current_line"
       exp_continue 
    }
  }
  eof { puts "eof"; exit 1; }
  timeout { puts "timeout"; exit 1; }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top