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

expect: Parse variables from stdin

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

是否有 way/pattern 解决了从标准输入读取并存储 (?) 某处相关输入的概念,以便在后面的步骤中 processed/parsed?

示例:

./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