2010-07-21 104 views
0

我寫下了腳本。期望命令輸出被存儲在一個變量中


#! /usr/bin/expect 
set timeout 180 
spawn /vobs/iov/rnc/bin/moshell/moshell -d db.dat 
expect { 
    -re "OFFLINE_DB.DAT.*" { } 
     timeout  { 
    error "\n######## Timeout - when logging in\n" 
} 
     eof   { 
    error "\n######## eof - when logging in\n" 
} 
} 

set db_prompt "SQL>" 
send "select id from motype_r1 where data = 'PlugInUnit';\r" 
expect { 
    -re "OFFLINE_DB.DAT>" 
}  
exit 

現在,我想表的輸出變量即

+------+ 
| id | 
+------+ 
| 19 | 
+------+ 
Query Done: 1 record selected 

並匹配正則表達式中多了一個varible得到 '19'。

任何人都可以請幫助我的解決方案。

/Akshya

+0

歡迎來到StackOverflow。請使用代碼標籤來設置您的代碼示例的格式,並向該問題添加更多信息。還要在您的問題中添加相關標籤。 – 2010-07-21 12:36:20

回答

1

在這個代碼塊,你應該能夠使用正則表達式的SELECT查詢的輸出相匹配,然後存儲它的變量。

send "select id from motype_r1 where data = 'PlugInUnit';\r" 
expect { 
    -re {(\|[^\d]*(\d+).*\|)} { set id $expect_out(1,string) ; exp_continue } 
    -re "OFFLINE_DB.DAT>" 
} 

(原諒有點醜陋的正則表達式,我用,但應與return語句最後一個數字ID)。

$expect_out(1,string)是指在正則表達式的第一個字符串匹配,則exp_continue將導致期望繼續期望輸出,直到它看到「OFFLINE_DB.DAT」消息(順便說一句,我不認爲需要前綴-re)。

希望工程!

相關問題