Tcl 为什么子进程的状态为非零?

Tcl 为什么子进程的状态为非零?,tcl,Tcl,考虑以下代码: set status [catch {eval exec $Executable $options | grep "(ERROR_|WARNING)*" >@ stdout} errorMessage] if { $status != 0 } { return -code error "" } 如果子进程中出现错误,它们将在标准输出中输出。但如果子进程中没有错误,则状态值仍然非零。如何避免这种情况 还有一些方法可以代替bash grep使用fileuti

考虑以下代码:

set status [catch {eval exec $Executable $options | grep "(ERROR_|WARNING)*" >@ stdout}  errorMessage] 

if { $status != 0 }  {
    return -code error ""
} 
如果子进程中出现错误,它们将在标准输出中输出。但如果子进程中没有错误,则状态值仍然非零。如何避免这种情况

还有一些方法可以代替bash grep使用
fileutil::grep

如果子进程中出现错误,它们将在标准输出中输出。但如果子进程中没有错误,则状态值仍然非零。如何避免这种情况

向任何文件描述符(包括连接到“standadd错误流”的描述符)写入内容与返回非零退出代码之间没有任何联系,因为就操作系统而言,这些概念是完全独立的。进程完全可以不执行I/O并返回非零退出代码(Unix守护进程的一种常见情况,它通过syslog
syslog
)记录所有内容,包括错误),或者在标准错误流中写入某些内容,并在退出时返回零-这是软件的常见情况,该软件将某些有价值的数据写入其
stdout
,并在请求时向其
stderr
提供诊断消息

因此,首先验证您的进程没有写入任何标准错误,并且仍然使用纯shell以非零退出代码退出

$ that_process --its --command-line-options and arguments if any >/dev/null
$ echo $?
(进程应不打印任何内容,
echo$?
应打印非零数字)

如果该情况为真,并且您确信流程不会认为有问题,则必须使用
catch
解决该问题,并处理-忽略流程使用已知退出代码退出并每隔一个错误传播一次的情况

基本上:

set rc [catch {exec ...} out]
if {$rc != 0} {
    global errorCode errorInfo
    if {[lindex $errorCode 0] ne "CHILDSTATUS"} {
        # The error has nothing to do with non-zero process exit code
        # (for instance, the program wasn't found or the current user
        # did not have the necessary permissions etc), so pass it up:
        return -code $rc -errorcode $errorCode -errorinfo $errorInfo $out
    }
    set exitcode [lindex $errorCode 2]
    if {$exitcode != $known_exit_code} {
        # Unknown exit code, propagate the error:
        return -code $rc -errorcode $errorCode -errorinfo $errorInfo $out
    }
    # OK, do nothing as it has been a known exit code...
}
描述了
CHILDSTATUS
(以及
errorCode
全局变量)

如果子进程中出现错误,它们将在标准输出中输出。但如果子进程中没有错误,则状态值仍然非零。如何避免这种情况

向任何文件描述符(包括连接到“standadd错误流”的描述符)写入内容与返回非零退出代码之间没有任何联系,因为就操作系统而言,这些概念是完全独立的。进程完全可以不执行I/O并返回非零退出代码(Unix守护进程的一种常见情况,它通过syslog
syslog
)记录所有内容,包括错误),或者在标准错误流中写入某些内容,并在退出时返回零-这是软件的常见情况,该软件将某些有价值的数据写入其
stdout
,并在请求时向其
stderr
提供诊断消息

因此,首先验证您的进程没有写入任何标准错误,并且仍然使用纯shell以非零退出代码退出

$ that_process --its --command-line-options and arguments if any >/dev/null
$ echo $?
(进程应不打印任何内容,
echo$?
应打印非零数字)

如果该情况为真,并且您确信流程不会认为有问题,则必须使用
catch
解决该问题,并处理-忽略流程使用已知退出代码退出并每隔一个错误传播一次的情况

基本上:

set rc [catch {exec ...} out]
if {$rc != 0} {
    global errorCode errorInfo
    if {[lindex $errorCode 0] ne "CHILDSTATUS"} {
        # The error has nothing to do with non-zero process exit code
        # (for instance, the program wasn't found or the current user
        # did not have the necessary permissions etc), so pass it up:
        return -code $rc -errorcode $errorCode -errorinfo $errorInfo $out
    }
    set exitcode [lindex $errorCode 2]
    if {$exitcode != $known_exit_code} {
        # Unknown exit code, propagate the error:
        return -code $rc -errorcode $errorCode -errorinfo $errorInfo $out
    }
    # OK, do nothing as it has been a known exit code...
}

CHILDSTATUS
(以及通常的
errorCode
全局变量)被描述。

<如果没有错误,代码>捕获应该返回
0
<如果没有错误,code>catch应该返回
0
。。。?