【问题标题】:Send command output in shell expect在 shell 中发送命令输出期望
【发布时间】:2017-12-12 18:44:59
【问题描述】:

以下是我尝试在 shell 脚本中使用 expect 执行的代码。

send "date +%d%m%y \n"  
set date1 "$expect_out(buffer)"  
puts "$date1"  
expect "#"  

我得到的输出是

date +%d%m%y  
111217

有什么方法可以抑制命令(date +%d%m%y) 并且只将输出存储在变量 date1 中?

【问题讨论】:

标签: expect


【解决方案1】:

试试这个:期望看到 6 个数字作为一个完整的单词

send "date +%d%m%y\r"         ;# typically we use \r to "hit enter"
expect -re {\m(\d{6})\M}
set date1 "$expect_out(1,string)"  
puts "$date1"  
expect "#"  

更一般地说,要选择第一行(命令)和最后一行(提示)之间的文本,您可以这样做:

lassign [regexp -inline {.*?\n(.*)\r\n#} $expect_out(buffer)] _ date1

其中_ 是一个一次性变量,它将保存正则表达式的整个匹配文本,而date1 将只保存捕获的文本。

请参阅https://tcl.tk/man/tcl8.6/TclCmd/re_syntax.htm 了解 Tcl 正则表达式语法。


没有lassign,你可以写

regexp {.*?\n(.*)\r\n#} $expect_out(buffer) _ date1

【讨论】:

  • lassign 在我的系统中不起作用。有没有其他方法可以只获取命令的输出?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多