使用 tcl 获取已执行的 sipp 的退出代码

getting exit code of an executed sipp with tcl

在使用 tcl 的 exec 执行 SIPp 命令后,我试图获取退出代码“echo $?”:

if { [catch { exec /usr/local/sipp-3.3.990/sipp -t "u1" -s $SERVICE \
                                -ap $PASSWORD \
                            -sf $SCRIPT_PATH1 \
                            -i $LOCAL_IP \
                            -m 1 -key path $str \
                            -key dnid *20 \
                            -timeout 10s\
                            -trace_err -trace_screen $CLIENT2 -nd | /bin/sh $str/return_code.sh } fid option] }

我曾尝试执行 "echo $?" 但那没有用,所以我尝试从另一个名为 "return_code.sh"

的脚本执行它
  #!/bin/sh
echo "exit code: $?"
echo "PPID: $PPID"

但这也不起作用,结果始终为 0,关于如何从 tcl 获取退出代码的任何想法?

此致,

详尽的示例在 http://wiki.tcl.tk/1039#pagetoce3a5e27b

处给出

这可能更容易

set status [catch {sipp ...} output option]

if {$status == 0} {
    # sipp did not emit anything to stderr, and exited with status 0
} else {
    # some error
    set err_info [lassign [dict get $option -errorcode] err_type]
    switch -exact -- $err_type {
        NONE {
            # sipp exited with status 0 but printed something to stderr
            # which is captured in $output
        }
        CHILDSTATUS {
            # non-zero exit status
            set exit_status [lindex $err_info 1]
            set process_id  [lindex $err_info 0]
        }
        default {
            puts "some other error: $option"
        }
    }
}

啊,我明白你在做什么了(你提供的代码太复杂了,无法清楚地展示)。你正在做 sipp .... | sh -c 'echo exit code: $?'。该 echo 命令将始终为零:它是 运行 在尚未启动任何命令的单独进程中。示例:

% set status [catch {exec sh -c {exit 42} | sh -c {echo "previous exit status: $?"}} output option]
1
% puts $output
previous exit status: 0
child process exited abnormally
% set option
-code 1 -level 0 -errorcode {CHILDSTATUS 25539 42} -errorinfo {previous exit status: 0
child process exited abnormally
    while executing
"exec sh -c {exit 42} | sh -c {echo "previous exit status: $?"}"} -errorline 1

我们看到第二个 sh 进程的 $? 值为零。然而,管道的退出状态,如 Tcl 所见,是 42。