Sending Without Echoing
Many programs echo their input. For example, if you send the date
command to the shell, you will see the string date
followed by a date. More precisely, you will see everything that you would ordinarily see at a terminal. This includes formatting, too.
send "date\r" expect -re $prompt
The command above ends with expect_out(buffer)
set to "date\r\nSun Jun 13 18:54:11 EDT 1993\r\n%2
" (your date or prompt may be different). The %2
at the end is a typical prompt. More importantly, the string date
has been echoed. Also, each line ends with a \r\n
, including the one you sent with a \r
.
The echoing of date
has nothing to do with the send
command. To put this another way, there is no way to send the string and have send
not echo it because send
is not echoing it in the first place. The spawned process is.
In many cases, the spawned process actually delegates the task of echoing to the terminal driver, but the result is the same—you see your input to the process as output from the process.
Often, echoed input can be handled by accounting for it in patterns. Chapter 7 (p. 173) demonstrated a simple example of this. In general, when dealing with the shell, you can just search for the prompt, and you need not worry about anything that comes before it. One unfortunate possibility is that your command looks like a prompt. In this case, you have to adjust your pattern or take some other action. Another possibility is to change your prompt to something that will never resemble ...
Get Exploring Expect now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.