2015-04-06 29 views
2

我正在編寫一個期望腳本來自動執行Sophos安裝。這裏是什麼樣子:期望腳本不識別「--MORE--」,之後沒有運行任何命令

#!/usr/bin/expect 

set installdir [lindex $argv 0] 
set timeout 20 

spawn "./install.sh" 

expect { 
    sleep 5 
    "Press <return> to display Licence. Then press <spc> to scroll forward." {send "\r"} 
    -ex "--More--" {send -- " "; exp_continue} 
    "Do you accept the licence? Yes(Y)/No(N)\\\[N\\\]" {send "Y"} 
    "Where do you want to install Sophos Anti-Virus? \\\[/opt/sophos-av\\\]" {"send $installdir/sophos-av"} 
    "Do you want to enable on-access scanning? Yes(Y)/No(N) \\\[Y\\\]" {"send \r"} 
    "Do you want to enable remote management? Yes(Y)/No(N) \\\[Y\\\]" {"send \r"} 
    "Username for Sophos Anti-Virus GUI? \\\[admin\\\]" {"send \r"} 
    "Password for Sophos Anti-Virus GUI?" {"********"} 
    "Re-enter the same password." {"********"} 
} 

interact 

當我運行expect腳本,它進入的許可證的按鍵,但什麼都不做,當第一「 - 更多 - 」提示出現,並運行沒有後續命令。思考?想法?

+0

「睡眠5」不是睡眠命令:expect會尋找睡眠模式,如果發現執行命令「5」。如果你真的想睡覺,把命令放在expect命令的上面。 – 2015-04-06 13:21:30

回答

0

您是否期望程序依次查看這些模式中的每一個?如果是這樣,你需要使用多個連續的期望和發送命令:

sleep 5 
expect "Press <return> to display Licence. Then press <spc> to scroll forward." 
send "\r" 
expect { 
    -ex "--More--" {send -- " "; exp_continue} 
    "Do you accept the licence? Yes(Y)/No(N)\\\[N\\\]" 
} 
send "Y" 
expect "Where do you want to install Sophos Anti-Virus? \\\[/opt/sophos-av\\\]" 
send "$installdir/sophos-av\r" 
expect "Do you want to enable on-access scanning? Yes(Y)/No(N) \\\[Y\\\]" 
send "\r" 
expect "Do you want to enable remote management? Yes(Y)/No(N) \\\[Y\\\]" 
send "\r" 
expect "Username for Sophos Anti-Virus GUI? \\\[admin\\\]" 
send "\r" 
expect "Password for Sophos Anti-Virus GUI?" 
send "********\r" 
expect "Re-enter the same password." 
send "********\r" 
interact 

一旦expect執行的「動作」塊中,預計命令將返回。它不會等待與其他模式相匹配,除非您告訴它(exp_continue

有幫助的提示:在開發預期腳本時,請在腳本頂部附近打開詳細的調試輸出:exp_internal 1

+0

所以我嘗試了上述方法,它肯定會對--MORE--做出正確反應,但它永遠不會移動到下一個命令上。 – 2015-04-07 16:21:02

+0

當然(事後)。 「預期」需要等待其他事情擺脫「更多」循環。更新。 – 2015-04-07 16:22:45

+0

我明白了!非常感謝你們的幫助。 :) – 2015-04-07 16:48:08