[Linux Dev:as ~]$ cat expectsh1.sh
echo "hi"
echo "world"
echo "hello"
[Linux Dev:as ~]$ cat expectshmain.sh
#!/usr/bin/expect
spawn "./expectsh1.sh"
expect "hello" { send "12r" }
expect "hi" { send "23r" }
interact
获取异常,如下所示:
[Linux Dev:as ~]$ ./expectshmain.sh
spawn ./expectsh1.sh
hi
world
hello
12
spawn_id: spawn id exp6 not open
while executing
"interact"
(file "./expectshmain.sh" line 8)
谁能告诉我问题的原因和解决办法吗?在shell中使用交互命令的目的是什么? 目的是将生成的程序的控制权交给键盘上的人。在这种情况下,在您期望"hello"之后,生成的程序就完成运行了。
如果您实际上不想与生成的程序交互,如果您只是希望期望脚本干净地退出,请使用expect eof
如果您的目标是演示expect的使用,请考虑这样重写您的脚本:
cat >expectsh1 <<'EOF'
#!/bin/sh
echo "hi"
read -r first_num
echo "world"
read -r second_num
echo "You entered $first_num and then $second_num"
# ...and do something with further human interaction
while read -r; do
echo "You entered: $REPLY"
done
EOF
cat >expect-test <<'EOF'
#!/usr/bin/expect
spawn "./expectsh1"
expect "hello" { send "12r" }
expect "hi" { send "23r" }
interact
EOF