Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I flush the input buffer in an expect script?

Tags:

ksh

expect

I'm writing an Expect script and am having trouble dealing with the shell prompt (on Linux). My Expect script spawns rlogin and the remote system is using ksh. The prompt on the remote system contains the current directory followed by " > " (space greater-than space). A script snippet might be:

send "some command here\r"
expect " > "

This works for simple commands, but things start to go wrong when the command I'm sending exceeds the width of the terminal (or more precisely, what ksh thinks is the width of the terminal). In that case, ksh does some weird horizontal scrolling of the interactive command line, which seems to rewrite the prompt and stick an extra " > " in the output. Naturally this causes the Expect script to get confused and out of sync when there appears to be more than one prompt in the output after executing a command (my script contains several send/expect pairs).

I've tried changing PS1 on the remote system to something more distinctive like "prompt> " but a similar problem arises which indicates to me that's not the right way to solve this.

What I'm thinking might help is the ability for the script to tell Expect that "I know I'm properly synchronised with the remote system at this point, so flush the input buffer now." The expect statement has the -notransfer flag which doesn't discard the input buffer even if the pattern does match, so I think I need the opposite of that.

Are there any other useful techniques that I can use to make the remote shell behave more predictably? I understand that Expect goes through a lot of work to make sure that the spawned session appears to be interactive to the remote system, but I'd rather that some of the more annoying interactive features (such as the horizontal scrolling of ksh) be turned off.

like image 366
Greg Hewgill Avatar asked Dec 09 '09 21:12

Greg Hewgill


2 Answers

If you want to throw away all output Expect has seen so far, try

expect -re $

This is a regexp match on $ which means the end of the input buffer, so it will just skip everything received so far. More details at the Expect man page.

like image 135
Colin Macleod Avatar answered Sep 18 '22 09:09

Colin Macleod


You could try "set -o multiline" or COLUMNS=1000000 (or some other suitably large value).

like image 42
Andru Luvisi Avatar answered Sep 22 '22 09:09

Andru Luvisi