tags:

views:

929

answers:

3

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.

+1  A: 

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

Glomek
The `ksh` on this system doesn't seem to have the `multiline` option, but I've discovered that if I `set +o emacs` (turning off the emacs line editing option, which appears to be the default here) then the horizontal scrolling is also disabled.
Greg Hewgill
A: 

I have had difficulty with ksh and Expect in the past. My solution was to use something other than ksh for a login shell.

If you can change the remote login to other than ksh (using the chsh command or editing /etc/passwd) then you might try this with /bin/sh as the shell.

Another alternative is to tell KSH that the terminal is a dumb terminal - disallow it from doing any special processing.

$ export TERM=""

might do the trick.

Philip Schlump
Unfortunately, in this situation a lot of other things depend on the shell being ksh. I'll try unsetting `TERM`, that's a good idea.
Greg Hewgill
+3  A: 

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.

Colin Macleod
Excellent, that appears to be just what I needed.
Greg Hewgill

related questions