2011-04-27 83 views
2

我有以下期待腳本需要從expect腳本

expect -c " 
spawn tdwallet add $StringName 
expect \"^Enter\" 
send \"Krishna123\r\" 
" 

if [ $? != 0 ]; then 
    echo "Failed to Add a String $StringName." 
else 
    echo "\"add <string>\" ==> [OK]" 
fi 

我試圖修改expect腳本,即增加了一些行之後「送」

expect -c " 
spawn tdwallet add $StringName 
expect \"^Enter\" 
send \"Krishna123\r\" 
expect eof 
catch wait reason 
exit [lindex $reason 3] 
" 

但它是返回催生procees的退出代碼無論過程失敗/成功,總是返回一個非零值。

當你的程序失敗/成功時,你能給我一些指出正確退出代碼的指針嗎?

在此先感謝

問候,
Srikrishna地震署。

回答

3

問題是$reason正在被您的shell替換(而不是期望)。而且,它可能被替換爲空字符串,這會導致語法錯誤。解決這個問題的最快捷的方法是把一個反斜槓在$面前,就像這樣:

expect -c " 
spawn tdwallet add $StringName 
expect \"^Enter\" 
send \"Krishna123\r\" 
expect eof 
catch wait reason 
exit [lindex \$reason 3] 
" 

OTOH,它可能會更好,現在你起牀到這種複雜性的把那個劇本在一個單獨的文件,所以你可以減少反斜槓的數量。此時,您將切換到將命令行添加爲命令行參數。

因此,在addWallet.exp你有這樣的:

spawn tdwallet add [lindex $argv 0] 
expect "^Enter" 
send "Krishna123\r" 
expect eof 
catch wait reason 
exit [lindex $reason 3] 

然後你的主腳本是這樣的:

expect addWallet.exp $StringName 

這是更容易與合作!