Bashで動的入力を期待します。

Bashで動的入力を期待します。

次の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"

スクリプトは、シェルスクリプトに対する応答ボットの役割を期待します。それでは、いくつかの答えを動的に生成する必要があるとしましょう。たとえば、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

おすすめ記事