spawn后 send 最后都追加一个回车符\r。因为这是程序之间的交互,用的是回车。
但是没有spawn的情况下,send后面追加的是\n。这是方便输出到终端的操作。
参考:
expect "hi\n"
send "hello there!\n"
hi and then pressed return. My input matched the pattern " hi\n ". Ideally, a return would be matched with " \r "; however, the UNIX terminal driver translates a return to " \n ". 2 As you will see later on, it is rarely necessary to have to worry about this mapping because most of Expect's interactions occur with programs not users, and programs do not "press return". Nonetheless, it is occasionally useful to expect input from people. Plus, it is much easier to experiment with Expect this way.
我输入的字符串hi,然后按下回车。我的输入匹配模式“hi\n”。理想情况下,回车与“\r”相匹配; 然而,UNIX终端驱动将回车转换成"\n"。正如你将在后面看到,很少需要担心这个映射,因为大部分Expect的交互发生在程序中而不是用户侧,程序并不”按回车“。尽管如此,在期望从用户输入时这偶尔是有用的,另外,这是很容易使用Expect进行实验。
expect "Name"
send "anonymous\r"
expect "Password:"
send "[email protected]\r"
expect "ftp> "
It is a common mistake to terminate send commands to a process followed by \n . In this context, \n denotes a linefeed character. You do not interactively end lines with a linefeed. So Expect must not either. Use " \r ".
这是一个常见的错误去使用\n来终止关联到一个进程的send命令。在这种情况下,\n表示换行字符。你不会使用换行符结束一行。所以Expect来不会。这里使用“\r”。
Contrast this to what I was doing earlier -- sending to a user, or rather, standard output. Such strings were indeed terminated with a \n . In that context, the \n denotes a newline. Because standard output goes to a terminal, the terminal driver translates this to a carriage-return linefeed sequence.
Similarly, when reading lines from a program that would normally appear on a terminal, you will see the carriage-return linefeed sequence. This is represented as \r\n in an expect pattern.
将这个和我之前做的相对比 - 发送给用户,或者更确切地说,标准输出。这样的字符串确实是使用\n来终止的。在这种情况下,\n表示新的行。由于标准输出到终端,终端驱动将其转换成回车换行序列。
同样,在终端上从程序读入行的时候,你会看到回车换行序列。这表示为Expect模式中的\r\n。
This may seem confusing at first, but it is inherent in the way UNIX does terminal I/O and in the representation of characters and newlines in strings. The representation used by Tcl and Expect is common to the C language and most of the UNIX utilities. I will have more to say on the subject of newlines and carriage returns in .
第一眼看起来似乎是令人困惑的,但它是固有的UNIX处理终端I/O的方式,和字符串中的字符和换行符的表示方式。使用Tcl和Expect的表示是和C语言和大部分的UNIX工具是共通的。我将在换行和回车的主题中做更多的阐述。