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. nginx访问fastdfs文件 报错400 Bad Request

    1.修改vi /etc/fdfs/mod_fastdfs.conf 2.将url_have_group_name = false 改为 url_have_group_name = true 3.重启 ...

  2. Docker搭建EFK日志收集系统,并自定义es索引名

    EFK架构图 一.EFK简介 EFK不是一个软件,而是一套解决方案,并且都是开源软件,之间互相配合使用,完美衔接,高效的满足了很多场合的应用,是目前主流的一种日志系统. EFK是三个开源软件的缩写,分 ...

  3. Docker:Docker部署postgresql数据库

    环境 Centos-7  Postgresql-10 docker-19 yum加载Postgresql yum install https://download.postgresql.org/pub ...

  4. Java:Java的^运算符详解

    按位异或运算符(^)是二元运算符,要化为二进制才能进行计算,在两个操作数中,如果两个相应的位相同,则运算结果为0,否则1:例如:int a=15:int b=a^8;运算结果为:a=15:b=7:a( ...

  5. SpringBoot:SpringBoot中@Value注入失败

    1. 第一步检测语法是否正确 @Value("${test}") private String test; 2.第二步检测配置文件中是否有进行配置 url=testusername ...

  6. mysql中的空值

    空值,即NULL,在数据库中通过is null 和is not null来查询 陷阱一:空值不一定为空 空值是一个比较特殊的字段.在MySQL数据库中,在不同的情形下,空值往往代表不同的含义.这是My ...

  7. Linux | 管首命令符号

    简介 管道的意思,在我们日常生活中,意思就是运输一个东西,到下一个地方,所以说 管道命令符 的使用也是差不多的,也是运送一段数据到下一个地方,格式:命令A | 命令B | 命令C .... 所以说,管 ...

  8. 修改gitlab默认的nginx

    目录 1. 修改gitlab的配置文件 2. nginx配置 3. 重载 前言: 本文将介绍,如何禁用gitlab自带的nginx,用已经安装的nginx提供web服务. 1. 修改gitlab的配置 ...

  9. 「CF527E」 Data Center Drama

    「CF527E」 Data Center Drama 传送门 显然一个环肯定满足题目条件. 然后我就开始想:先整一棵 \(\texttt{DFS}\) 树,然后非树边从深度深的节点向深度浅的节点连边, ...

  10. 记录APP 启动ACTIVITITY

    a.启动待测apkb.开启日志输出:adb logcat>D:/log.txt c.关闭日志输出:ctrl+cd.查看日志找寻: Displayed com.mm.android.hsy/.ui ...