先贴代码 主要我用一个shell脚本实现增加linux用户的功能
1 #!/usr/bin/expect -f
2 #!/bin/sh
3 set password 1
4 set name yiyi
5 set num ""
6 spawn adduser $name
7 set timeout 30;
8 expect {
9 password: {send $password\n;exp_continue};
10 password: {send $password\n;exp_continue};
11 *[]: {send $num\n;exp_continue};
12 *[]: {send $num\n;exp_continue};
13 Phone: {send $num\n;exp_continue};
14 Phone: {send $num\n;exp_continue};
15 Other: {send $num\n;exp_continue};
16 }
17 set timeout 30
18 expect "Is the information correct?"
19 send "Y\r"
20 set timeout 30;
21 send exit\n;
22 expect eof
执行完就以报如下错误求大神解决啊:
expect: spawn id exp6 not open
while executing
"expect "Is the information correct?""
(file "./adduser.sh" line 18)
因为第8行开始的expect里面包含exp_continue,所以这个expct {}(8-16行)的行为是个loop,会一直run 到 eof ,
再继续执行18行的expect的时候,已经没有input了,就打印id exp6 not open的错误。
所以需要把后面的expect合并到这个block里面。
18行的expect、19行的send,以及21行的send都应该跟8行的epxect放在同一个exepect {}block里面?
第6行的命令有些没有进入到expect中去,也就是没有执行expect部分,然后就输入expect eof,才会报错
spawn id exp6 not open
原因
脚本没有被执行
解决方案
手动执行该条命令参看报错的原因