gpt4 book ai didi

linux - 如何迭代多个预期行

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:46:13 27 4
gpt4 key购买 nike

我正在尝试解析 CLI 的一些输出并对其进行迭代。输出类似于以下内容,我想遍历每个 id 以对这些对象执行更多操作。

OVM> list ServerPool
Command: list ServerPool
Status: Success
Data:
id:123456789 name:pool1
id:987654321 name:pool2

我正在尝试以下代码,但由于某种原因它在打印第二个 ID 后挂起。我觉得跟exp_continue有关系,不过我不太理解expect。此外,我正在为只有 2 个 ID 的情况做这个简单的解决方案,因为我不知道如何概括它并一次获取多行以便稍后迭代它们并发送更多命令。

我尝试在打印第二个 id 后添加一个导出,但它没用,就像它试图继续期待东西并卡在那里一样。那时我不知道如何取消 exp_continue。

expect "OVM> " {
send "list ServerPool\r"
expect {
-re " id:(.*?) (.*?)\n\r" {
send_user "$expect_out(1,string)\n"; exp_continue
}
-re " id:(.*?) (.*?)\n\r" {
send_user "\n$expect_out(1,string)\n";
}
}
}

send "exit\r"
expect eof

最佳答案

请看下面的例子:

% cat foo.exp
spawn -noecho cat file

set idNames {}
expect {
-re {id:([0-9]+) name:([[:alnum:]]+)} {
set idName [list $expect_out(1,string) $expect_out(2,string)]
lappend idNames $idName
exp_continue
}
"OVM>" {}
}

send_user "==== result: ====\n"
foreach idName $idNames {
lassign $idName id name
send_user "id=$id name=$name\n"
}
% cat file
OVM> list ServerPool
Command: list ServerPool
Status: Success
Data:
id:123456789 name:pool1
id:234567890 name:pool2
id:345678901 name:pool3
id:456789012 name:pool4
id:567890123 name:pool5
id:678901234 name:pool6
OVM> other command
% expect foo.exp
OVM> list ServerPool
Command: list ServerPool
Status: Success
Data:
id:123456789 name:pool1
id:234567890 name:pool2
id:345678901 name:pool3
id:456789012 name:pool4
id:567890123 name:pool5
id:678901234 name:pool6
OVM> other command
==== result: ====
id=123456789 name=pool1
id=234567890 name=pool2
id=345678901 name=pool3
id=456789012 name=pool4
id=567890123 name=pool5
id=678901234 name=pool6
%

关于linux - 如何迭代多个预期行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36852017/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com