假设以下bash
脚本questions.sh
:
#!/bin/bash
echo "Hello, who are you?"
read REPLY
和以下expect
脚本:
#!/usr/bin/expect -f
spawn ./questions.sh # start our script using spawn command
expect "Hello, who are you?\r"
send -- "Im Adam\r"
Expect 脚本充当 shell 脚本的应答机器人。现在假设我需要动态生成一些答案。例如,假设我想提供另一个 shell 脚本 ( answers.sh
) 的输出作为答案。我该如何修改expect
脚本?我尝试了以下方法,但似乎不起作用:
#!/usr/bin/expect -f
spawn ./questions.sh # start our script using spawn command
expect "Hello, who are you?\r"
send ./answers.sh
答案1
我想到了:
#!/usr/bin/expect -f
spawn ./questions.sh # start our script using spawn command
expect "Hello, who are you?\r"
set answer [exec ./answer.sh]
send $answer