Bash 使用expect自动执行两个脚本之间的交互

Bash 使用expect自动执行两个脚本之间的交互,bash,shell,expect,Bash,Shell,Expect,我有两个简单的脚本,我需要它们使用第三个expect脚本自动交互 script1.sh #/bin/bash 回显“s1:已启动” 回应“问题1” 阅读 echo“获得${REPLY}” 回应“问题2” 阅读 echo“获得${REPLY}” 回应“问题3” 阅读 echo“获得${REPLY}” 回声“s1:完成” script2.sh #/bin/bash 回显“s2:已启动” 阅读 echo“获得${REPLY}” 回应“回答1” 阅读 echo“获得${REPLY}” 回应“回答2”

我有两个简单的脚本,我需要它们使用第三个
expect
脚本自动交互

script1.sh

#/bin/bash
回显“s1:已启动”
回应“问题1”
阅读
echo“获得${REPLY}”
回应“问题2”
阅读
echo“获得${REPLY}”
回应“问题3”
阅读
echo“获得${REPLY}”
回声“s1:完成”
script2.sh

#/bin/bash
回显“s2:已启动”
阅读
echo“获得${REPLY}”
回应“回答1”
阅读
echo“获得${REPLY}”
回应“回答2”
阅读
echo“获得${REPLY}”
回应“回答3”
回声“s2:完成”
auto\u s1\u s2.exp

#/usr/bin/expect
spawn-noecho./script1.sh
设置s1ID$spawn\u id
spawn-noecho./script2.sh
设置s2ID$spawn\u id
期望-i$s1ID“问题1”{
发送用户“exp:question1\n”
发送-i$s2ID“问题1\r”
期望-i$s2ID“回答1”{
发送用户“exp:answer1\n”
发送-i$s1ID“应答器1\r”
}
}
互动
然而,运行
/auto\u s1\u s2.exp
的输出是:

s1: started
question1
exp: question1
question1
s2: started
got question1
answer1
exp: answer1

我希望
s1
会回显
question1
,这是根据
expect$s1ID“question1”
进行验证的,然后expect脚本会将
question1
发送到
s2
并期望
answer1
然后将
answer
发送到
s1
,依此类推

script1

我在这里做错了什么?

如前所述,expect脚本工作正常,但没有显示输出,因为interact没有侦听特定的spawn_id

这里是一个完整的示例

外壳部分: 预期部分: 结果: 请举一个例子:

结果:

[bash] # bash qa.sh
s1: started
s2: started
question1
question1
got question1
answer1
answer1
got answer1
question2
question2
got question2
answer2
answer2
got answer2
question3
question3
got question3
answer3
s2: finished
s2: finished
got s2: finished
s1: finished
[bash] #

其他人无法从您的脚本中猜到您将要做什么,因为它没有按照您的预期工作。你能详细说明一下你的目的吗?@pynexj我添加了更多信息
script1
确实得到了
answer1
,但你的interact命令不会显示它。您需要告诉它来监听spawn id:
interact-i$s1ID
Expect源代码中有一个示例程序,演示了两个棋类程序相互作用:您的答案产生了正确的结果,但我无法理解正则表达式在这里的行为
Expect-d qa.exp
可以提供详细信息。
[STEP 104] $ cat qa.exp
spawn bash s1.sh
set s1 $spawn_id

spawn bash s2.sh
set s2 $spawn_id

expect -i $s1 "s1: started"
expect -i $s2 "s2: started"

expect {
    -i $s1 -re "(question.)" {
        set q $expect_out(1,string)

        send -i $s2 "$q\r"
        expect -i $s2 -re "got\[^\r\n]+\[\r\n]+"
        expect -i $s2 -re "(\[^\r\n]+)\[\r\n]+$"
        set a $expect_out(1,string)

        send -i $s1 "$a\r"

        exp_continue
    }
    -i $s1 eof {
        exp_continue
    }
    -i $s2 eof {
    }
}
[STEP 105] $ expect qa.exp
spawn bash s1.sh
spawn bash s2.sh
s1: started
question1
s2: started
question1
got question1
answer1
answer1
got answer1
question2
question2
got question2
answer2
answer2
got answer2
question3
question3
got question3
answer3
s2: finished
s2: finished
got s2: finished
s1: finished
[STEP 106] $
[bash] # cat qa.sh
sock1=/tmp/s1.$$.sock
sock2=/tmp/s2.$$.sock

sexpect -s $sock1 spawn bash s1.sh
sexpect -s $sock1 expect 's1: started'

sexpect -s $sock2 spawn bash s2.sh
sexpect -s $sock2 expect 's2: started'

while true; do
    sexpect -s $sock1 expect -re '(question.)'
    ret=$?
    if sexpect chkerr -err $ret -is eof; then
        break
    fi
    q=$( sexpect -s $sock1 expect_out -i 1 )

    sexpect -s $sock2 send -cr "$q"
    sexpect -s $sock2 expect -re $'got[^\r\n]+[\r\n]+'
    sexpect -s $sock2 expect -re $'([^\r\n]+)[\r\n]+$'
    a=$( sexpect -s $sock2 expect_out -i 1 )

    sexpect -s $sock1 send -cr "$a"
done

sexpect -s $sock1 wait
sexpect -s $sock2 wait
[bash] #
[bash] # bash qa.sh
s1: started
s2: started
question1
question1
got question1
answer1
answer1
got answer1
question2
question2
got question2
answer2
answer2
got answer2
question3
question3
got question3
answer3
s2: finished
s2: finished
got s2: finished
s1: finished
[bash] #