在Bash脚本中使用Expect / TCL的变量(_ _ _处缺少操作数)

时间:2016-02-09 12:02:09

标签: bash tcl expect

Hej all,

我想在bash脚本中使用expect代码。有关背景信息,我获得了期望脚本的解决方案:Save terminal output to variable in expect/tcl

当我单独使用它时脚本工作正常。但我想在bash脚本中使用它来处理多个文件,我对expect部分中的变量有问题。

我收到了这些错误:

set new error bounds? (0: no, 1: yes): missing operand at _@_
in expression "  _@_> 0.3 ||  > 0.3 "
(parsing expression "  > 0.3 ||  > 0.3 ")
...

代码:

#! /bin/bash
MLI_offs=$1 
MLI_snr=$2
MLI_diff_par=$3

/usr/bin/expect <<EOF
spawn offset_fitm $MLI_offs $MLI_snr $MLI_diff_par MLI_coffs MLI_coffsets 7.0 6 1
set range 1.5
set azimuth 1.5

while {true} {
expect "enter minimum SNR threshold:"
send "7.0\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.3 || $azimuth > 0.3 } {
        send "1\r"
    } else {
        send "0\r" 
        break
    }
}
}
interact

EOF

谢谢, 比约恩

2 个答案:

答案 0 :(得分:1)

问题是变量在bash和Tcl / Expect中被替换。由于bash用空字符串替换未知变量,这会留下一个完全错误的脚本(反过来又会抱怨,因为它无法弄清楚正在发生什么)。它也会破坏其他东西(使用expect_out),但是它发生了你没有击中它。

最简单的事情是停止使用bash作为包装器,因为Tcl非常擅长通过argv全局来做这类事情。因此:

#! /usr/bin/expect
set MLI_offs [lindex $argv 0]
set MLI_snr [lindex $argv 1]
set MLI_diff_par [lindex $argv 2]
# Alternatively, replace the preceding three lines with:
#    lassign $argv MLI_offs MLI_snr MLI_diff_par

spawn offset_fitm $MLI_offs $MLI_snr $MLI_diff_par MLI_coffs MLI_coffsets 7.0 6 1
set range 1.5
set azimuth 1.5

while {true} {
    expect "enter minimum SNR threshold:"
    send "7.0\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.3 || $azimuth > 0.3 } {
            send "1\r"
        } else {
            send "0\r" 
            break
        }
    }
}
interact

答案 1 :(得分:0)

您可以对所有Expect(tcl)变量使用特殊的$来转义为'\',以防止Bash解释它。例如。 $range应该在您的Bash脚本中写为\$range