获取TCL中open创建的进程的return代码
Get the return code of the process created by open in TCL
现在我正在通过打开调用外部 bash 脚本,因为所述脚本可能 运行 几秒钟或 运行 几分钟。唯一可以确定的是:
- 它将输出必须显示给用户的文本。不是在脚本完成之后,而是还在 运行ning 期间。
- 会设置一个return不同含义的代码
阅读和使用 shell 脚本输出的文本确实有效。但是我不知道如何阅读 return 代码。
(简化的)TCL 脚本如下所示:
#!/usr/bin/tclsh
proc run_script {} {
set script "./testing.sh"
set process [open "|${script}" "r"]
chan configure $process -blocking 0 -translation {"lf" "lf"} -encoding "iso8859-1"
while {[eof $process] == 0} {
if {[gets $process zeile] != -1} {
puts $zeile
}
update
}
close $process
return "???"
}
set rc [run_script]
puts "RC = ${rc}"
(简化的)shell 脚本看起来像这样:
#!/bin/bash
echo Here
sleep 1
echo be
sleep 2
echo dragons
sleep 4
echo ....
sleep 8
exit 20
那么如何通过 tcl 读取 shell 脚本的 return 代码?
在关闭文件描述符以获取退出代码之前,您需要将文件描述符切换回阻塞状态。例如:
您可以使用 try ... trap
,它是用 tcl 8.6 实现的:
chan configure $process -blocking 1
try {
close $process
# No error
return 0
} trap CHILDSTATUS {result options} {
return [lindex [dict get $options -errorcode] 2]
}
另一种选择是使用 catch
:
chan configure $process -blocking 1
if {[catch {close $process} result options]} {
if {[lindex [dict get $options -errorcode] 0] eq "CHILDSTATUS"} {
return [lindex [dict get $options -errorcode] 2]
} else {
# Rethrow other errors
return -options [dict incr options -level] $result
}
}
return 0
要获取 8.5 中的状态,请使用:
fconfigure $process -blocking 1
if {[catch {close $process} result options] == 1} {
set code [dict get $options -errorcode]
if {[lindex $code 0] eq "CHILDSTATUS"} {
return [lindex $code 2]
}
# handle other types of failure here...
}
要获取 8.4 中的状态,请使用:
fconfigure $process -blocking 1
if {[catch {close $process}] == 1} {
set code $::errorCode
if {[lindex $code 0] eq "CHILDSTATUS"} {
return [lindex $code 2]
}
# handle other types of failure here...
}
现在我正在通过打开调用外部 bash 脚本,因为所述脚本可能 运行 几秒钟或 运行 几分钟。唯一可以确定的是:
- 它将输出必须显示给用户的文本。不是在脚本完成之后,而是还在 运行ning 期间。
- 会设置一个return不同含义的代码
阅读和使用 shell 脚本输出的文本确实有效。但是我不知道如何阅读 return 代码。
(简化的)TCL 脚本如下所示:
#!/usr/bin/tclsh
proc run_script {} {
set script "./testing.sh"
set process [open "|${script}" "r"]
chan configure $process -blocking 0 -translation {"lf" "lf"} -encoding "iso8859-1"
while {[eof $process] == 0} {
if {[gets $process zeile] != -1} {
puts $zeile
}
update
}
close $process
return "???"
}
set rc [run_script]
puts "RC = ${rc}"
(简化的)shell 脚本看起来像这样:
#!/bin/bash
echo Here
sleep 1
echo be
sleep 2
echo dragons
sleep 4
echo ....
sleep 8
exit 20
那么如何通过 tcl 读取 shell 脚本的 return 代码?
在关闭文件描述符以获取退出代码之前,您需要将文件描述符切换回阻塞状态。例如:
您可以使用 try ... trap
,它是用 tcl 8.6 实现的:
chan configure $process -blocking 1
try {
close $process
# No error
return 0
} trap CHILDSTATUS {result options} {
return [lindex [dict get $options -errorcode] 2]
}
另一种选择是使用 catch
:
chan configure $process -blocking 1
if {[catch {close $process} result options]} {
if {[lindex [dict get $options -errorcode] 0] eq "CHILDSTATUS"} {
return [lindex [dict get $options -errorcode] 2]
} else {
# Rethrow other errors
return -options [dict incr options -level] $result
}
}
return 0
要获取 8.5 中的状态,请使用:
fconfigure $process -blocking 1
if {[catch {close $process} result options] == 1} {
set code [dict get $options -errorcode]
if {[lindex $code 0] eq "CHILDSTATUS"} {
return [lindex $code 2]
}
# handle other types of failure here...
}
要获取 8.4 中的状态,请使用:
fconfigure $process -blocking 1
if {[catch {close $process}] == 1} {
set code $::errorCode
if {[lindex $code 0] eq "CHILDSTATUS"} {
return [lindex $code 2]
}
# handle other types of failure here...
}