使用tcl/expect检测断言(0)

使用tcl/expect检测断言(0),tcl,expect,assert,Tcl,Expect,Assert,这是我的test.cpp程序。它通过断言(0)异常退出 但是当我试图在tcl/expect中检测异常退出时,我似乎无法: #!/usr/bin/expect spawn ./test expect eof lassign [wait] pid spawnid os_error_flag value if {$os_error_flag != 0} { puts "OS error" exit 1 } if {$value != 0} { puts "Application err

这是我的
test.cpp
程序。它通过
断言(0)
异常退出

但是当我试图在
tcl/expect中检测异常退出时,我似乎无法:

#!/usr/bin/expect

spawn ./test
expect eof
lassign [wait] pid spawnid os_error_flag value

if {$os_error_flag != 0} {
  puts "OS error"
  exit 1
}
if {$value != 0} {
  puts "Application error"
  exit 1
}

puts "No error"
当我运行该脚本时:

$ ./test.expect
No error

如果我使用
exit(1)
而不是
assert(0)
,那么tcl脚本能够检测异常退出。为什么
tcl/expect
不为断言失败提供
OS-
应用程序返回的
错误代码,以及如何通过检查退出代码来统一检测所有异常程序退出?

不是答案,而是扩展注释:

运行该代码,我看到:

$ ./a.out; echo $?
Assertion failed: (0), function main, file x.c, line 4.
Abort trap: 6
134
在expect中,我看到:

$ expect
expect1.1> spawn ./a.out
spawn ./a.out
47429
expect1.2> expect eof
Assertion failed: (0), function main, file x.c, line 4.
expect1.3> wait
47429 exp6 0 0 CHILDKILLED SIGABRT SIGABRT

看起来您需要查看第4个之后由
wait
返回的元素。

不是答案,而是扩展注释:

运行该代码,我看到:

$ ./a.out; echo $?
Assertion failed: (0), function main, file x.c, line 4.
Abort trap: 6
134
在expect中,我看到:

$ expect
expect1.1> spawn ./a.out
spawn ./a.out
47429
expect1.2> expect eof
Assertion failed: (0), function main, file x.c, line 4.
expect1.3> wait
47429 exp6 0 0 CHILDKILLED SIGABRT SIGABRT
看起来您需要查看第4个之后由
wait
返回的元素。

当我查找时,我看到:

其他元素可能出现在wait返回值的末尾。可选的第五个元素标识一类信息。目前,该元素唯一可能的值是childkill,在这种情况下,接下来的两个值是C样式的信号名称和简短的文本描述

assert()
调用使用
abort()
在断言失败时终止进程,并通过SIGABRT显示为退出

set result [wait]
if {[lindex $result 4] eq "CHILDKILLED"} {
    if {[lindex $result 5] eq "SIGABRT"} {
        # assertion failed, or other abort
    } else {
        # other signal
    } 
} else {
    # normal exit, may be with conventional error
}
错误处理绝对可以很精细

当我抬头看时,我看到:

其他元素可能出现在wait返回值的末尾。可选的第五个元素标识一类信息。目前,该元素唯一可能的值是childkill,在这种情况下,接下来的两个值是C样式的信号名称和简短的文本描述

assert()
调用使用
abort()
在断言失败时终止进程,并通过SIGABRT显示为退出

set result [wait]
if {[lindex $result 4] eq "CHILDKILLED"} {
    if {[lindex $result 5] eq "SIGABRT"} {
        # assertion failed, or other abort
    } else {
        # other signal
    } 
} else {
    # normal exit, may be with conventional error
}
错误处理绝对可以很精细