how to clean up the previous expect_buf in an expect script
Asked Answered
B

1

8

I've written a expect function to get the output of a command and my code is like below

proc do_cmd {cmd id} {
  set spawn_id $id
  send "$cmd\r"
  expect "$cmd\r"
  expect {
    -re "\n(.*)\r\n" {return $expect_out(1,string)}
    default {exit 1}
  }
}

If I call the function just once it would works fine and return something I want, but if I call it continually without a break, it would return something unwanted.

# test case 1
set ret [do_cmd $mycmd $spawn_id]
puts "$mycmd returns $ret"  # the return value is ok

# test case 2
set ret [do_cmd $mycmd $spawn_id]
set ret [do_cmd $mycmd $spawn_id]
puts "$mycmd returns $ret"  # the return value is not something I want

I use the 'exp_internal 1' to debug it and found that the expect_out in the second called command still holds the previous output info and caused the matched problem, so how can I clean up the expect_out buffer(I tried to set it an empty string but it doesn't work,) or is there anything else I can do to avoid this problem? Thanks in advance.

Boilermaker answered 27/8, 2014 at 9:20 Comment(2)
Don't you need -re "\n(.*)\r\n" to match for regular expression ? because you are using expect_out(1,string)Abstention
Oops, my mistake, thank you for remind me thatBoilermaker
A
12

Don Libes's suggestion for your scenario is as follows,

Sometimes it is even useful to say:

expect *

Here the * matches anything. This is like saying, "I don't care what's in the input buffer. Throw it away." This pattern always matches, even if nothing is there. Remember that * matches anything, and the empty string is anything! As a corollary of this behavior, this command always returns immediately. It never waits for new data to arrive. It does not have to since it matches everything.

Reference : Exploring Expect

In this case, after your required match, better try to save the match to some variable then simply add the code expect * at the last. This will empty the buffer. Your code can altered as below.

proc do_cmd {cmd id} {
  set spawn_id $id
  send "$cmd\r"
  #Looks like you are looking for a particular command to arrive
  expect "$cmd\r"
  #Then you have one more expect here which is you want to get it
  expect {
    #Saving the value sub match to the variable 'result'
    -re "\n(.*)\r\n" {set result $expect_out(1,string)}}
    }
  #Causing the buffer to clear and it will return quickly
  expect *
  return $result
}

Apart from this, there is one more way can be unsetting the expect_out(buffer) content itself which will remove the 'buffer' index from expect_out array which can be depicted as

unset expect_out(buffer)

When the next match happens, expect_out array will be updated the index 'buffer' and we can have the fresh expect_out(buffer) value. Replace the expect * with the above code if you prefer to use this way.

This is quite a workaround kind of stuff to get what we want actually. You can go ahead with any approach. Choice is yours. :)

Abstention answered 29/8, 2014 at 8:2 Comment(1)
I've determined empirically that unsetting the expect_out(buffer) is much more consistent than "expect *". This post was of great help for me. Stackoverflow is indispensable like always.Kilt

© 2022 - 2024 — McMap. All rights reserved.