2010-07-21 107 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)。

希望工程!

相关问题