gpt4 book ai didi

linux - 期望:从标准输入解析变量

转载 作者:太空宇宙 更新时间:2023-11-04 09:16:18 27 4
gpt4 key购买 nike

我正在运行一个 expect 脚本,它将生成一些来自 stdin 的动态输入。

是否有一种方法/模式可以解决从标准输入读取并将相关输入存储(?)到某处以在后面的步骤中处理/解析的概念?

示例:

./myexpectscript.sh arg1 arg2 ..

Running command with id 9494
Running command with id 9494
Running command with id 9494
Running command with id 9494
Command execution finished

其实我想存储上面的id,9494

该脚本实际上对远程服务器运行一个 api 调用,持续时间为几秒(重要的是)。

编辑:以下代码段似乎无法解决问题:

expect -re Running command with id [0-9]+

set output \$expect_out(1,string)

因为它给我一个错误:

invalid command name "0-9"
while executing
"0-9"
invoked from within
"expect -re Running command with id [0-9]+"
(file "./myexpectscript.sh" line 17)

也尝试过用引号,即

expect -re "Running command with id [0-9]+"

最佳答案

使用 {...}要定义您的正则表达式,请使用 \d对于十进制数,并使用 (...) 捕获字符串.

$expect_out 捕获的字符串设置一个变量.

expect -re {Running command with id (\d+)} {
set cmd_id $expect_out(1,string)
}
puts $cmd_id

关于linux - 期望:从标准输入解析变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47285559/

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