【问题标题】:How do I extract a certain number from an Expect buffer string?如何从 Expect 缓冲区字符串中提取某个数字?
【发布时间】:2012-04-24 13:54:21
【问题描述】:

我想通过使用 Expect 脚本来获取主机的总内存。感谢我在Get Total Memory of a host with LINUX/EXPECT 中收到的答案,我现在离最终解决方案更近了。 (感谢glenn jackmanSorpigal)。

连接到主机后,我执行以下行:

send "cat /proc/meminfo | grep MemTotal | awk '{print \$2}'\r"

它返回我想要的值,但是当我得到 $expect_out(buffer) 中包含的值时,它包含的信息比我需要的要多得多,包括发送语句和前后命令提示符标志。使用exp_internal 1,我完全得到以下信息:

expect: set expect_out(buffer) "17# cat /proc/meminfo | grep MemTotal | awk '{print $2}'\r\n34150400\r\nCBA"

我只想提取接近末尾的数字34150400。有什么提示或想法吗?

【问题讨论】:

    标签: expect


    【解决方案1】:
    regexp "\n(\[0-9]+)" $expect_out(buffer) - num
    puts $num   ;# => 34150400
    

    顺便说一句,你可以在不发送 shell 命令的情况下做到这一点:

    set fid [open /proc/meminfo]
    while {[gets $fid line] != -1} {
        if {[regexp {^MemTotal: *(\d+)} $line - memtotal]} {
            break
        }
    }
    close $fid
    puts $memtotal
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-22
      • 2022-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多