Expect command output to be stored in a variable
I have written below script .
#! /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
Now , I want to get the output of the table in a variable i.e
+------+
| id |
+-----开发者_StackOverflow中文版-+
| 19 |
+------+
Query Done: 1 record selected
and match regular expression to get '19' in one more varible.
Can anybody please help me with the solution.
/Akshya
In this block of code, you should be able to use a the regex to match the output of the SELECT query, then store it in a variable.
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>"
}
(Pardon the somewhat ugly regexp that I used, but it should match the last numerical id in the return statement.)
The $expect_out(1,string)
refers to the first string match in the regex, then the exp_continue will cause expect to continue expecting output until it sees the "OFFLINE_DB.DAT" message (which I don't think needs the -re
prefix, by the way).
Hope that works!
精彩评论