General_Twyckenham
General_Twyckenham

Reputation: 2261

Expect conditionals don't seem to be working

Edit: Everything works perfectly without the conditional. Adding in the conditional breaks the script. I've tried conditionals in other simpler scripts and those have worked.

Edit2: Adding the full script as other instances of expect conditionals have worked

I'm trying to modify an autoexpect generated expect script that works when the build succeeds but doesn't handle the build failing.

After significant research, I can't figure out why my expect conditional isn't working

#!/usr/bin/expect -f

set force_conservative 0  ;# set to 1 to force conservative mode even if
                          ;# script wasn't run conservatively originally
if {$force_conservative} {
        set send_slow {1 .1}
        proc send {ignore arg} {
                sleep .1
                exp_send -s -- $arg
        }
}

set timeout -1
spawn $env(SHELL)
match_max 100000
expect "~\$ "
send -- "sudo su\r"
expect "/home/ubuntu# "
send -- "cd ../../opt/application/"
send -- "\r"
expect  "/opt/application# "
send -- "./buildwar.sh \r"
expect  {
        "BUILD SUCCESSFUL\r" {
                send -- "su appuser\r"
                expect  "/opt/application\$ "
                send -- "../../home/ubuntu/shutdown.sh \r"
                expect  "\"outcome\" => \"success\"}"
                send -- "exit\r"
                expect "/opt/application# "
                send -- "./deploywar.sh \r"
                expect "BUILD SUCCESSFUL"
                send -- "su appuser\r"
                expect  "/opt/application\$ "
                send -- "../../home/ubuntu/startup.sh \r"
                expect "Deployed \"application.war\""
                send -- "exit\r"
                send -- "exit\r"
                send -- "exit\r"
                exit 0
        }

        "BUILD FAILED\r" {
                exit 1
        }
}

Upvotes: 2

Views: 410

Answers (3)

General_Twyckenham
General_Twyckenham

Reputation: 2261

The issue was that nesting expect statements doesn't seem to work, so instead of the posted code, the following works:

#!/usr/bin/expect -f

set force_conservative 0  ;# set to 1 to force conservative mode even if
                          ;# script wasn't run conservatively originally
if {$force_conservative} {
        set send_slow {1 .1}
        proc send {ignore arg} {
                sleep .1
                exp_send -s -- $arg
        }
}

set timeout -1
spawn $env(SHELL)
match_max 100000
expect "~\$ "
send -- "sudo su\r"
expect "/home/ubuntu# "
send -- "cd ../../opt/application/"
send -- "\r"
expect  "/opt/application# "
send -- "./buildwar.sh \r"
expect  {
        "BUILD SUCCESSFUL\r" {
                send -- "su appuser\r"
        }

        "BUILD FAILED\r" {
                exit 1
        }
}
expect  "/opt/application\$ "
send -- "../../home/ubuntu/shutdown.sh \r"
expect  "\"outcome\" => \"success\"}"
send -- "exit\r"
expect "/opt/application# "
send -- "./deploywar.sh \r"
expect "BUILD SUCCESSFUL"
send -- "su appuser\r"
expect  "/opt/application\$ "
send -- "../../home/ubuntu/startup.sh \r"
expect "Deployed \"application.war\""
send -- "exit\r"
send -- "exit\r"
send -- "exit\r"

Upvotes: 2

glenn jackman
glenn jackman

Reputation: 246774

You may need to run with expect debugging on (expect -d). You don't actually show what data you are expecting.

It works for me:

$ expect -c '
  log_user 0
  spawn sh -c {echo "BUILD SUCCESSFUL"}
  expect {
          "BUILD SUCCESSFUL\r" {
                  #various and sundry other code to run
                  exit 123
          }

          "BUILD FAILED\r" {
                  exit 56
          }
  }
'
$ echo $?
123
$ expect -c '
  log_user 0
  spawn sh -c {echo "BUILD FAILED"}
  expect {
          "BUILD SUCCESSFUL\r" {
                  #various and sundry other code to run
                  exit 123
          }

          "BUILD FAILED\r" {
                  exit 56
          }
  }
'
$ echo $?
56

Upvotes: 2

yacc
yacc

Reputation: 3361

You need to match against \n - though it won't matter if you just omit it.

Upvotes: 1

Related Questions