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

时间:2015-03-27 14:18:51

标签: tcl exec echo tshark

我试图获取退出代码"回声$? "从tcl:

执行带有exec的SIPp命令后
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获取退出代码的任何想法?

致以最诚挚的问候,

1 个答案:

答案 0 :(得分:2)

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。