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

标签 linux bash expect

我正在运行一个 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/

相关文章:

linux - "bash:no job control in this shell” 是什么意思?

linux - Expect - 中断程序 - Ctrl+C

linux - 在linux中读取expect文件中的输出

linux - 如何仅grep包含x和y的内容?

bash - 错误: Macro % has illegal name (%define) whenever I run yum

c - 第二次 recv 调用不接收数据,在 C 中停止执行

linux - 如何查找给定名称以外的文件?

linux - 期望脚本中的 sudo 问题

linux - 使用来自另一个目录的相对路径运行脚本

Java SSH 从另一台远程机器连接到远程机器