内容预知
3.3.5 结束符(expect eof 与 interact 只能二选一)
1. Here Document免交互
交互:当计算机播放某多媒体程序的时候,编程人员可以发出指令控制该程序的运行,而不是程序单方面执行下去,程序在接受到编程人员相应的指令后而相应地做出反应。
对于Linux操作系统中,有许多操作都会触及到交互(根据系统的指示做出相对应的操作满足操作者的需求),对于shell脚本的自动化运维,就要实现免交互来达到自动化运维的效果
常用的交互程序:read,ftp,passwd,su,sudo,fdisk等等
cat也可配合免交互的方式重定向输出到文件。
Here Document的作用:
语法格式:
命令 <<开始标记
.......................
......................
结尾标记
使用须知:
- [root@localhost jiaohu]#read a <<EOF
- > this is a test
- > hello world
- > EOF
- [root@localhost jiaohu]#echo $a
-
- [root@localhost jiaohu]#read a <<jiewei
- > this is a test2
- > jiewei
- [root@localhost jiaohu]#echo $a

- [root@localhost jiaohu]#wc -l <<EOF
- > aaaaa
- > bbbbb
- > ccccc
- > ddddd
- > eeeee
- > ddddd
- > EOF

- [root@localhost jiaohu]#passwd <<EOF
- > 12341234
- > 12341234
- > EOF

cat 查看免交互输入内容 :
- [root@localhost jiaohu]#cat <<EOF
- > my name is zhangsan
- > hello world
- > EOF

cat 查看交互内容并输出到新的文件中 :
- [root@localhost jiaohu]#cat <<EOF>test.txt
- > hello world
- > i im zhangsan
- > EOF
- [root@localhost jiaohu]#cat test.txt

- [root@localhost jiaohu]#tee test2.txt <<EOF
- > this is tee test
- > EOF
- this is tee test
- [root@localhost jiaohu]#cat test2.txt

2. Here Document 变量的使用
Here Document也支持变量的使用。
如果标记之间有变量被使用,会先替换变量值。如果想要将一些内容写入文件,除了常规的方法外,也可以使用 Here Document。
如果写入的内容中包含变量,在写入文件时要先将变量替换成实际值,再结合cat命令完成输出。
- #!/bin/bash
-
- var="this is test1"
-
- var1=$(cat <<EOF
- this is test2
- this is test3
- $var
- EOF
- )
- echo "$var1"

输出结果:

- #!/bin/bash
-
- var="this is test1"
-
- var1=$(cat <<'EOF'
- this is test2
- this is test3
- $var
- EOF
- )
- echo "$var1"

输出结果:

使用冒号为开头的Here Document,是多行注释。输入的内容不会被执行。(一使用#进行注释更为直观,这个了解就行了)

输出结果:

解决方法:
- #!/bin/bash
-
- cat <<-EOF
- this is a test3
- EOF
-

新的输出结果:

3. Expect实现免交互运用
Expect是建立在 tcl 语言基础上的一个工具,常被用于进行自动化控制和测试,解决shell脚本中交互相关的问题。
需要安装两个软件包:expect,tcl。但是安装了expect后就会有tcl(yum解决依赖关系就安装了tcl)
建立在 tcl 语言基础上的一个工具,常被用于进行自动化控制和测试,解决 shell 脚本中交互相关的问题
检查expect安装包:
rpm -q expect
检查依赖包tcl:
rpm -q tcl
安装下载expect工具
yum install -y expect #安装expect(tcl软件包会作为依赖被安装)
expect 脚本中首先引入文件,表明使用的是哪一个 shell

spawn 后面通常跟一个 Linux 执行命令,表示开启一个会话、启动进程,并跟踪后续交互信息。
1)expect eof
2)interact
expect 默认的超时时间是10秒,通过set 命令可以设置会话超时时间,若不限制超时时间则应设置为-1
表示回显命令,相当于echo
expect 脚本可以接受从bash命令行传递参数,使用 [lindex $argv n]获得。其中你从0开始,分别表示第一个,第二个,第三个.....参数
4.Expect免交互的脚本运用
注意:expect脚本不能通过bash、source、. 来执行(因为这三种方式是调用shell解释器),只能通过绝对路径或相对路径来执行。
原本的交互过程:

脚本编写:
- #!/usr/bin/expect
-
- #该脚本用于实现免交互修改zhangsan用户的密码
-
- set timeout 5
-
- spawn passwd zhangsan
-
- expect "新的 密码"
- send "123abcd\r"
- expect "重新输入新的 密码"
- send "123abcd\r"
-
- expect eof
脚本实现:


脚本实现结果:
嵌入执行模式,将expect过程融入Shell 当中,方便执行和处理;但是像ssh、su这种会切换环境的交互式命令一般不建议使用嵌入执行模式。
- #!/bin/bash
- username=$1
- password=$2
- /usr/bin/expect << EOF
- set timeout 5
- spawn passwd $username
- expect "新的 密码"
- send "$password\n"
- expect "重新输入新的 密码"
- send "$password\n"
-
- expect eof
- EOF

执行结果:

原交互过程:
脚本编写:
- #!/usr/bin/expect
-
- set timeout 1
-
- set username [ lindex $argv 0 ]
- set password [ lindex $argv 1 ]
-
- spawn su $username
- expect "密码"
- send "$password\n"
-
- expect "*]#"
- send_user "$username 切换成功!"
-
- interact

执行结果:
原交互过程:

脚本编写:
- #!/usr/bin/expect
-
- set timeout 5
- set hostname [ lindex $argv 0 ]
- set password [ lindex $argv 1 ]
-
- spawn ssh $hostname
- expect {
- "No route to host" exit
- "Connection refused" exit
- "(yes/no)" {send "yes\n" ; exp_continue}
- "password:" {send "$password\n"}
- }
- interact

执行结果:

原交互过程:

脚本编写:
- #!/usr/bin/expect
-
- set timeout 5
- set name [lindex $argv 0]
-
- spawn fdisk $name
- expect "获取帮助"
- send "n\r"
- expect "Select"
- send "p\r"
- expect "分区号"
- send "\r"
- expect "起始"
- send "\r"
- expect "Last"
- send "+10G\r"
- expect "命令"
- send "w\r"
-
- interact

执行结果:
总结
1.交互式的操作方便用户进行单一设置的进行,但是面对批量设置就需要脚本接入,免交互就式解决脚本对其执行的问题
2.因为expect 的工具的运用,所以脚本的解释器声明要发生改变,或则使用嵌入式(但是嵌入式不适合ssh,su这样能够改变操作环境的交互式场景命令)
3.编写免交互的脚本,需要先对该交互过程,尽可能考虑每一种出现的交互因素,来确保脚本的实用性和完整性