2015-10-20 67 views
2

編輯:沒有條件,一切正常工作。在條件中添加可以打破腳本。我試過其他更簡單的腳本中的條件和那些工作。期望條件似乎沒有工作

EDIT2:添加完整的腳本的其他實例想到條件句工作過

我試圖修改autoexpect產生期望腳本時生成成功,但不處理構建失敗的作品。

顯著的研究後,我想不通爲什麼我的期望條件是不工作

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

你成功退出1? – chrisaycock

+0

@chrisaycock是的 - 這種方式調用這個期望腳本的shell腳本可以繼續它的其他職責 –

+0

@General_Twyckenham:壓力是在'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" 
1

您需要對陣\n - 但如果你只是忽略它,它都不會有問題。

+0

對不起,我是不太確定我明白你的答案。我在哪裏添加'\ n'? –

+0

這裏:''建立成功\ n「' – yacc

+0

這沒有用。正如我前面所說的,當不嘗試條件語句時,腳本完全匹配'「BUILD SUCCESSFUL \ r」',所以我懷疑'\ n'會改變任何東西 –

2

您可能需要在預期調試時運行(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 
+0

哦,很酷,這真的很有用。我正在查找的數據正好是「」BUILD SUCCESSFUL \ r「'(在記錄完整的其他評論之後)。使用-d標誌運行後,它看起來像條件​​預期行不被理解爲一個命令 - 它不檢查該點的輸出 –