首页 > 其他分享 >expect 中的回车和换行

expect 中的回车和换行

时间:2023-05-04 19:32:19浏览次数:42  
标签:return 换行 send UNIX expect 回车 Expect


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工具是共通的。我将在换行和回车的主题中做更多的阐述。



标签:return,换行,send,UNIX,expect,回车,Expect
From: https://blog.51cto.com/u_16097040/6244262

相关文章

  • 如何在 Python 中不换行打印?
    一般来说,从C/C++切换到Python的人想知道如何打印两个或多个变量或语句而不切换到python中的换行符。因为pythonprint()函数默认以换行符结尾。如果你使用print(a_variable)Python有一个预定义的格式,它会自动转到下一行。例如:print("cafedev")print("cafedevscafedev"......
  • 如何在 Python 中不换行打印?
    一般来说,从C/C++切换到Python的人想知道如何打印两个或多个变量或语句而不切换到python中的换行符。因为pythonprint()函数默认以换行符结尾。如果你使用print(a_variable)Python有一个预定义的格式,它会自动转到下一行。例如:print("cafedev")print("cafedevscafedev"......
  • LINUX 串口设备行规程——回车检测机制
    做项目时调用串口,遇到了只有存在回车时才能read到数据的情况,这是设置了行规程的结果。禁用行规程即可取消按行读取机制,如下:(建议大部分情况下都禁用)newtio.c_lflag&=~ICANON;......
  • Markdown表格-换行、合并单元格
    1.表格中对其、换行处理1.1一般我们都会这样用表格如下:|排名|姓名||--|--||第一名|隔壁老王||第二名|隔壁小王、隔壁小小王|实现效果图:排名姓名第一名隔壁老王第二名隔壁小王、隔壁小小王1.2当然这里也可以通过设置|:--|左对齐,|--:|右对齐比如下面的......
  • matlab设置坐标轴颜色以及标签换行显示、旋转显示
    set(gca,'ycolor',[000]);%颜色%设置不同字体、字型、以及换行显示,换行显示用{}包住每行字即可yr=ylabel('\it\fontname{TimesNewRoman}y_{CS}^{M4}...\rm\fontname{TimesNewRoman}\cdot{10}');%旋转跳跃set(yr,'Rotation',0,'position',[5.71100]......
  • NPOI Word 换行
    NPOI版本2.6.0需求描述:多行文本换行问题,如下图模板,其中$质量标准$,会被多行文本替换,不管是用\r\n、AddCarriageReturn、AddBreak()等各种方法,要么不好使;要么word转pdf文件后,pdf换行好用,但是word不是换行;开发就要做到表里如一,word与pdf一致,翻看官方示例后,尝试了示例中的各种换行......
  • 【软件工具使用问题】gitlab中md文件如何显示换行
    前言 解决方法:在需要换行的文本后面,多加两个空格就可以。 参考1. GitHub中README.md如何换行_newnwee的博客-CSDN博客;完......
  • css--实现一个文字少时居中,文字换行时居左的样式
    css--实现一个文字少时居中,文字换行时居左的样式 前言最近群里的小伙伴去面试,遇到这样一个问题,面试官问:"用css对一行文字进行布局,当文字不够换行的时候,这行文字要居中显示,当文字出现换行的时候,这行文字要靠左显示。",遇到这样的需求一下束手无策,后来查下资料,哦,原来这......
  • equal symbol expected
    这样嵌套会异常<html:textproperty="dateCreated"value=<bean:writename="dateCreated"property="dateCreated"/>/>像下边这样写就不会<html:textproperty="dateCreated"value="${dateCreated......
  • npm安装yarn报npm ERR Unexpected token ‘.‘错误的解决办法
    问题背景最近发现很多前端框架或前端产品的学习和使用都用到了yarn,一开始不知道这是干什么的,后在网上搜索说是JS包管理工具,我一致都是用的nodeJs,难道node过时了,然后有一顿搜索,网上是这么介绍的。Yarn是什么?“Yarn是由Facebook、Google、Exponent和Tilde联合推出了一个新的JS......