通过交互式响应动态响应期望

时间:2016-11-22 22:01:13

标签: linux shell unix command expect

我希望使用unix expect命令来包装eb init

这是我想要的API。

eb-init.exp --region=eu-central-1 --app=my-app --env=my-app-live

这是我当前的脚本:

set timeout -1
spawn $env(SHELL)
match_max 100000
send -- "eb init\r"
expect  "Select a default region"
send -- "5\r"
expect "Select an application to use"
send -- "1\r"
expect "Select the default environment"
send -- "1\r"
send -- "exit\r"
expect eof

我需要一种方法来与expect的响应进行交互,并使用命令中的输入参数对该行进行字符串处理。例如,如果以下是命令返回的内容,则它与expect "Select a default region"匹配,我想使用参数eu-central-1并获取数字5并动态响应。

Select a default region
1) us-east-1 : US East (N. Virginia)
2) us-west-1 : US West (N. California)
3) us-west-2 : US West (Oregon)
4) eu-west-1 : EU (Ireland)
5) eu-central-1 : EU (Frankfurt)
6) ap-south-1 : Asia Pacific (Mumbai)
7) ap-southeast-1 : Asia Pacific (Singapore)
8) ap-southeast-2 : Asia Pacific (Sydney)
9) ap-northeast-1 : Asia Pacific (Tokyo)
10) ap-northeast-2 : Asia Pacific (Seoul)
11) sa-east-1 : South America (Sao Paulo)
12) cn-north-1 : China (Beijing)
(default is 3):

这怎么可能?

1 个答案:

答案 0 :(得分:2)

试试这样:

set region "eu-central-1"
expect "Select a default region"
expect -re "\[\[:space:]]+(\[0-9]+)\\) $region *:"
expect "(default is *):"
send $expect_out(1,string)\r