在期望交互命令中获得异常

时间:2014-03-25 06:20:17

标签: linux bash shell expect

[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 "12\r" }
expect "hi" { send "23\r" }

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中使用interact命令的目的是什么??

2 个答案:

答案 0 :(得分:0)

目的是在键盘上控制产生的程序给人。在这种情况下,在您期望“hello”之后,生成的程序已完成运行。

如果您实际上不想与衍生程序进行交互,如果您只希望expect脚本完全退出,请使用expect eof

答案 1 :(得分:0)

如果您的目标是演示期望的使用,请考虑重写您的脚本:

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 "12\r" }
expect "hi" { send "23\r" }

interact
EOF