English 中文(简体)
How can I flush the input buffer in an expect script?
原标题:
  • 时间:2009-12-09 21:28:06
  •  标签:
  • 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
"
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.

最佳回答

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.

问题回答

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

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.





相关问题
How can I flush the input buffer in an expect script?

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 ...

changing to parent directory in unix

in general we use cd .. for going to the parent directory cd ../../ to go to the parents parent directory. and cd ../../../../../ for 5th parent directory. is there any simplified way of doing ...

$$ in KornShell

What is the meaning of $$ in KornShell? I guess it is associated with the process ID, but I want to know its exact meaning.

vi editor query

I use vi quite often to edit files. Usually I need the file to appear on screen with line numbers. For this I do set nu in edit mode. What a drag! Can I automate this? Is there any way where vi will ...

KornShell- Creating a fixed width text file

I need to create a simple fixed width text file in KornShell (ksh). My current attempt using printf to pad the string isn t working out very well. What s the shortest, cleanest way to create a fixed ...

KornShell Printf - Padding a string

I m attempting to write a KornShell (ksh) function that uses printf to pad a string to a certain width. Examples: Call padSpaces Hello 10 Output Hello I currently have: padSpaces(){ ...

Redirection to a strange file descriptor no

I came across a working ksh script [interactive] today, where I saw the below statement. printf "Enter the release no. : " >&5 I wonder the use of >&5 when the author could have as ...

热门标签