大家好,我必须使用一个交互式程序,并且我想用 expect 来自动化它。我在 Linux 和脚本方面的经验并不是最好的。所以这是程序输出:
enter minimum cross-correlation threshold: 0.15
enter the range and azimuth error thresholds: 1.6 1.0
range, azimuth error thresholds: 1.60000 1.00000
cross-correlation threshold: 0.15000
*
*
*
model fit std. dev. (samples) range: 0.5298 azimuth: 0.4166
set new error bounds? (0: no, 1: yes): 0
前两行和最后一行是交互式的。因此,使用 expect 和 send 一次性输入阈值不是问题。但我想重复这个过程。因此我必须将结果“范围:0.5298 方位角:0.4166”保存到两个变量中,然后再次运行,直到获得良好的结果,例如“范围:0.02 方位角:0.02”。
有人知道我如何存储结果、比较结果并重复该过程吗?
这样可行:
#!/usr/bin/expect
spawn ./offset_fitm_exp
expect "enter minimum cross-correlation threshold:" { send "0.15\r" }
expect "enter the range and azimuth error thresholds:" { send "1.6 1.0\r" }
expect "set new error bounds? (0: no, 1: yes):" { send "0\r" }
interact
谢谢你的帮助!Bjoern
答案1
为了自动化迭代,你可以这样做:
#!/usr/bin/expect
spawn ./offset_fitm_exp
set range 1.6
set azimuth 1.0
while {true} {
expect "enter minimum cross-correlation threshold:"
send "0.15\r"
expect "enter the range and azimuth error thresholds:"
send "$range $azimuth\r"
expect -re {range: ([0-9.]+) azimuth: ([0-9.]+} {
set range $expect_out(1,string)
set azimuth $expect_out(2,string)
}
expect "set new error bounds? (0: no, 1: yes):" {
if {$range > 0.02 || $azimuth > 0.02} {
send "1\r"
} else {
send "0\r"
break
}
}
}
interact
答案2
好的,我正在考虑,但我可能会很忙而忘记,或者偏离主题。所以您可以尝试将输出行处理为具有固定列的行(希望是这种情况)以匹配,并设置输出,您可以执行以下操作:
set A1 [lrange $expect_out(1,string) 2]
set A2 [lrange $expect_out(1,string) 3]
然后执行以下操作:
if {$A1>=N || $A2>=N} do something (like change the set a1,a2 values, or ....
我稍后会再回来查看。