Chapter 3

  • Section "The expect Command": expect_out(0,string) can NOT be written as "expect_out(0, string)", blank before "string" will make a mistake;

  • Concurrent matching:

expect {

item1 {send cmd1}

item2 {send cmd2}

}

  • In spawn process, use "\r" as the return key in send command;

Chapter 5

  • Section "Using Parentheses For Feedback" give a clear and concise explanations of how to get feedback in expect.

#!/usr/bin/expect
set timeout 60
expect -re "a(.*)c"
send "expect_out(buffer) = ${expect_out(buffer)}\n"
send "expect_out(0,string) = ${expect_out(0,string)}\n"
send "expect_out(1,string) = ${expect_out(1,string)}\n"
expect -re "e"
send "expect_out(buffer) = ${expect_out(buffer)}\n"
send "expect_out(0,string) = ${expect_out(0,string)}\n"
send "expect_out(1,string) = ${expect_out(1,string)}\n"

Run this script, input "junk abcbcdef" and return, will produces:

$ ./feedback.exp
junk abcbcdef
expect_out(buffer) = junk abcbc
expect_out(0,string) = abcbc
expect_out(1,string) = bcb
expect_out(buffer) = de
expect_out(0,string) = e
expect_out(1,string) = bcb

You can see value of "expect_out(...)" will be refreshed when a expect matches (expect "e"). "buffer" are all things from last buffer ("...cbc") to this match ("e"), which is "de" in this case. "0,string" means all matching strings (note "0, string" will raise a exception for the redundant blank). "1,string" means the first subgroup of "0,string". If there is no subgroup (expressed as parentheses) in current match, (1,string) remains last value ("bcb" in this case). Even "f" at the end of input has been sent to expect, it will not be added to "buffer", because it's not a part of the current match.

Chapter 6

  • exp_continue is useful in many circumstances. See its code example in section "Matching Multiple Times" in chapter 6 and section "Prompting For A Password On Behalf Of A Program" in chapter 8;

Chapter 7 & 8

  • some frequently used command:

log_file: write output of spawned process to a file;
log_user: send output of spawned process to/not to user;
send_user: expect script send messages to user;

expect_user: continue communicating with the user even after a process has been spawned;
send: expect script send messages to spawned process;
send_log: only write to log (without writing to stdout);
send_error: expect script send messages to stderr;
exp_internal: enable/disable internal diagnostics to stdout or log file (see section "Logging Internal Diagnostics");

  • proc sendexpect in section "The send_error Command" is useful;

  • right-wrong-timeout switch in section "The expect_user Command":

expect {

right {

} wrong {

send_error error_message

exit 1

} timeout {

send_error "time out!"

exit 1

}

}

  • A program switch between character and line mode. Notice the usage of "stty raw":

send_user "Now we are in line mode. You can use backspace to re-input.\n"
send_user "Continue? y or n\n"
expect_user -re "y|n"
send_user "you press $expect_out(0,string)\n"
stty raw
send_user "Now we are in character mode. You have no chance to re-input.\n"
send "Continue? Enter y or n: \r\n"
expect -re "y|n"
send_user "\nyou press $expect_out(0,string)"

  • stty should be executed during times when the user is not typing, such as before a prompt rather than after, otherwise there is possibility of losing characters while switching modes. See the end of section "Line Versus Character-Oriented And Other Terminal Modes";

  • In the modified version of su2 in section "Echoing", I modified "# " to " #" in the last 3rd line, because on my platform (Mint Xfce 14) there are no space after "#" in the prompt of root user;

  • the get password procedure in section "Echo" is a must-have tool;

  • If you want exact Bourne-shell semantics, the simplest way is to call system. See section "The system Command";

Chapter 9

arguments experiment after #!

The echo.exp:

version 1:

#!/usr/bin/expect
set argc [llength $argv]
for {set i 0} {$i<$argc} {incr i} {
puts "arg $i: [lindex $argv $i]"
}

version 2:

#!/usr/bin/expect --

...

version 3:

#!/usr/bin/expect -f

...

Now run command ./echo.exp -c "puts hello" 1 2 3, the output is:

version 1:

arg 0: -c
arg 1: puts hello
arg 2: 1
arg 3: 2
arg 4: 3

version 2:

arg 0: -c
arg 1: puts hello
arg 2: 1
arg 3: 2
arg 4: 3

version 3:

hello
arg 0: 1
arg 1: 2
arg 2: 3

So it is clear that only when "-f" is specified, "-c" means "take my arguments as a script". Otherwise "-c" will be treated as a common arguments of the script. "--" explicitly stop any arguments interpreting.

Chapter 17

  • this chapter describes how to make expect script a server;

Notes about "Exploring Expect"的更多相关文章

  1. Expect使用小记

    By francis_hao    May 31,2017   本文翻译了部分Expect的man手册,只选取了个人常用的功能,因此并不完善.   Expect是一个可以和交互式程序对话的程序 概述 ...

  2. /usr/bin/expect介绍

    /usr/bin/expect介绍 http://blog.csdn.net/zhu_tianwei/article/details/44180637 概述 我们通过Shell可以实现简单的控制流功能 ...

  3. (转) [it-ebooks]电子书列表

    [it-ebooks]电子书列表   [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Obj ...

  4. 交互式shell编程

    FQ #!/usr/bin/env shxfce4-terminal -x sudo python ./local/proxy.py 连续执行 gnome-terminal -x bash -c &q ...

  5. java 文件读写--转载

    读文件 http://www.baeldung.com/java-read-file Java – Read from File 1. Overview In this tutorial we’ll ...

  6. mkpasswd - 为用户产生新口令

    总览 SYNOPSIS mkpasswd [ args ] [ user ] 介绍 INTRODUCTION mkpasswd 为用户产生口令并自动应用.它是基于O'Reilly的书<Explo ...

  7. Exploring Python Code Objects

    Exploring Python Code Objects https://late.am/post/2012/03/26/exploring-python-code-objects.html Ins ...

  8. Go 1 Release Notes

    Go 1 Release Notes Introduction to Go 1 Changes to the language Append Close Composite literals Goro ...

  9. Android Weekly Notes Issue #227

    Android Weekly Issue #227 October 16th, 2016 Android Weekly Issue #227. 本期内容包括: Google的Mobile Vision ...

随机推荐

  1. printf 格式

    1.转换说明符      %a(%A)     浮点数.十六进制数字和p-(P-)记数法(C99)      %c         字符      %d         有符号十进制整数      % ...

  2. webpack(6)webpack处理图片

    图片处理url-loader(webpack5之前的处理方式) 在项目开发中,我们时长会需要使用到图片,比如在img文件夹中有图片test1.png,然后在normal.css中会引用到图片 body ...

  3. Linux 动态库 undefined symbol 原因定位与解决方法

    在使用动态库开发部署时,遇到最多的问题可能就是 undefined symbol 了,导致这个出现这个问题的原因有多种多样,快速找到原因,采用对应的方法解决是本文写作的目的. 可能的原因 依赖库未找到 ...

  4. ExtJs4学习(三)组件查找 ComponentQuery类

    Extjs3.x: ID:这就是所熟知的Ext.getCmp("组件ID"),缺点是id重复导致出错. ref:在EXTJS3中,所有的组件都会有一个ref属性,也就是refere ...

  5. shell下读取文件数据

    参考:https://www.imzcy.cn/1553.html while和for对文件的读取是有区别的: 1. for对文件的读是按字符串的方式进行的,遇到空格什么后,再读取的数据就会换行显示 ...

  6. webpack(11)配置文件分离为开发配置、生成配置和基础配置

    前言 上篇我们已经配置好了本地开发服务器,但是配置的相对比较凌乱,一个文件中有些是开发时用到的配置,有些是生成时用到的配置,有些是开发和生成都要用到的配置,所以我们这里把环境分为3个环境 webpac ...

  7. 第三章 深入理解python语言

    计算机技术的演进过程 1946-1981年 计算机系统结构时代(35年) 解决计算机能力的问题 1981-2008年 网络和视窗时代(27年) 解决交互问题 2008-2016年 复杂信息系统时代(8 ...

  8. Java 8 Function 函数接口

    这篇文章属于 Java 8 教程(LTS)系列教程 在 Java 8 中,Function 接口是一个函数接口,它位于包 java.util.function 下. Function 接口中定义了一个 ...

  9. CF175E Power Defence

    CF175E Power Defence 题意 一个塔防游戏:给定一个无限长的数轴,一个无限血的敌人要从正无穷走到负无穷.你的任务是放置三种塔,包含两种攻击塔和一种寒冰塔,使得敌人受到的伤害最大. 其 ...

  10. java高级编程笔记(四)

    java的Object类: 1.Object 类位于 java.lang 包中,编译时会自动导入:Java 的所有类都继承了 Object,子类可以使用 Object 的所有方法. 2.Object ...