期待条件似乎不起作用

时间:2015-10-20 16:11:18

标签: bash expect

编辑:没有条件,一切都很完美。添加条件会中断脚本。我已经在其他更简单的脚本中尝试过条件,并且已经有效了。

Edit2:添加完整脚本作为期望条件的其他实例已经有效

我试图修改autoexpect生成的expect脚本,该脚本在构建成功但不处理构建失败时有效。

经过重大研究,我无法弄清楚为什么我的期望条件不起作用

#!/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
        }
}

3 个答案:

答案 0 :(得分:2)

您可能需要运行expect {(expect -d)。您实际上并未显示您期望的数据。

它对我有用:

$ 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

答案 1 :(得分:2)

问题在于嵌套expect语句似乎不起作用,因此代替发布的代码,以下工作:

#!/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"

答案 2 :(得分:1)

你需要与\n匹配 - 尽管如果你省略它就不重要。