Sidd Thota
Sidd Thota

Reputation: 2249

Expect Script to wait for specific input for a certain amount of time

I'm working on Installation of a windows application via docker, which requires some user inputs like DB setup, etc. I'm using expect script for these user inputs to automate our installation.

But during the final step, My installation takes upto 15-20 mins as the application needs to install the DB schema and other required elements and then gives final input to press enter to exit the installation process.

How do I handle it, currently I'm just making my expect script to wait, but is there anyway for me to handle this and make expect script wait for that specific input "string match"?

here is how my expect script looks like

    #!/bin/bash

expect -c '
  spawn sh ./setupapp.sh
  expect "PRESS <ENTER> TO CONTINUE:"
  send "\r"
  expect "PRESS <ENTER> TO CONTINUE:"
  send "\r"
  expect "PRESS <ENTER> TO CONTINUE:"
  send "\r"
  expect "PRESS <ENTER> TO CONTINUE:"
  send "\r"
  expect "PRESS <ENTER> TO CONTINUE:"
  send "\r"
expect "PRESS <ENTER> TO CONTINUE:"
  send "\r"
  expect "Waiting here:"
  send "\r"
  expect "Waiting here:"
  send "\r"
  expect "Waiting here:"
  send "\r"
  expect "Waiting here:"
  send "\r"
  expect "Waiting here:"
  send "\r"
  expect "Waiting here:"
  send "\r"
 expect "PRESS <ENTER> TO Exit Installation:"
 send "\r"
  expect eof
'

I'm using Waiting here so that it wait for 10 seconds, is there any way that I can automate and wait for the last string Press Enter to Exit the Installation.

Thanks!

Upvotes: 1

Views: 2735

Answers (1)

glenn jackman
glenn jackman

Reputation: 246774

Looks like you want something like this: this is the most flexible way to expect something to happen multiple times, and we don't have to care about exactly how many times it happens.

expect -c '
  set timeout 1200   ;# 20 minutes
  spawn sh ./setupapp.sh
  expect {
    "PRESS <ENTER> TO CONTINUE:" {
      send "\r"
      exp_continue
    }
    "Waiting here:" {
      send "\r"
      exp_continue
    }
    timeout {
      error "nothing happened after $timeout seconds" 
    }
    "PRESS <ENTER> TO Exit Installation:" {
      send "\r"
    }
  }
  expect eof
'

That expect command waits for one of four things to happen. For the first 2, hit enter then keep waiting for another event. I added the "timeout" event in case you want to do something special there.

The "press enter to exit" block does not call "exp_continue". After it sends the carriage return, the enclosing expect command ends and we then wait for eof.

Upvotes: 1

Related Questions