ThomasReggi
ThomasReggi

Reputation: 59355

Respond to expect dynamically with the interactive response

I'm looking to use the unix expect command to wrap eb init.

Here's the API that I'd like.

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

Here's the current script that I have:

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

I need a way to interact with the response from expect and string the line with the input argument from the command. For example, if the following is what the command returns it is matched with expect "Select a default region", I'd like to use the argument eu-central-1 and get the number 5 and respond dynamically.

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):

How is this possible?

Upvotes: 1

Views: 77

Answers (1)

pynexj
pynexj

Reputation: 20688

Try like this:

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

Upvotes: 2

Related Questions