【问题标题】:TCL calling another proc after a procTCL 在一个 proc 之后调用另一个 proc
【发布时间】:2015-03-30 14:55:25
【问题描述】:

我有 2 个 procs,它们一个接一个地被调用。第一个过程使用diff 功能并创建file.txt。压缩文件后的proc。问题是当我运行脚本时,file.txt 是空的。当我注释掉压缩过程时,文件中印有差异。 我相信这是因为第二个 proc 没有等待第一个 proc 完成。

create_difference "file1" "file2" 
create_compress "path"

上述过程调用的顺序产生一个空文件.txt.gz

create_difference "file1" "file2" 
#create_compress "path"

上述过程调用的顺序创建了一个预期的差异文件。 在 procs 中,我尝试添加一个 return 语句(返回 1),这并没有什么不同。

我尝试使用wait 命令,如Waiting for background processes to finish before exiting script

create_difference "file1" "file2" 
wait
create_compress "path"

但是脚本在那个时候挂起。

创建差异的过程:tcl: capture output from "exec diff" which returned non-zero

set dso1 [open file.txt w]
set status [catch {exec diff $file1 $file2} result]
if {$status == 0} {
   puts $dso1 "$file1 and $file2 are identical"
} elseif {$status == 1} {
   puts $dso1 "** $file1 and $file2 are different **"
   puts $dso1 "***************************************************************************"
   puts $dso1 ""
   puts $dso1 $result
   puts $dso1 ""
   puts $dso1 "***************************************************************************"
} else {
   puts stderr "** diff exited with status $status **"
   puts stderr "***********************************************************************"
   puts stderr $result
   puts stderr "***********************************************************************"
}

压缩文件的过程:

proc create_compress  {thepath} {
        catch {exec find ${thepath}/. -type f -exec gzip "{}" \; } result
        #return 1
}

那里还有一些其他文件需要压缩,这就是为什么我压缩文件夹中的每个文件,给定文件夹的路径。其他文件按预期压缩。

我对其进行了更多测试。似乎即使在调用并完成了 diff proc 之后,差异也仅在脚本结束后才写入 file.txt。直到脚本结束,Differences 文件被创建,但它的大小是 0。

【问题讨论】:

  • 创建差异后再次运行程序,但这次反转 cmets 以便 create_compress 运行。文件还是空白还是压缩成功了?
  • 你如何使用差异?你能展示你如何从 Tcl 调用 diff 吗?
  • 我应该编辑我的帖子,文件已压缩(压缩有效),但它是空的
  • 这是一个语法错误:puts $dso1 stderr "** diff exited with status $status **" -- puts 的参数数量错误
  • 你没有后台进程,所以删除wait

标签: linux tcl wait pid


【解决方案1】:

没有写入差异文件的原因是它没有关闭。在我的 create_diff 过程中,我忘了包含命令:close $dso1 因此,一旦脚本完成运行,它才会写入文件。但是,在 diff proc 之后,我压缩了文件,由于它无法写入压缩文件,因此文件将为空。

Tldr;我没有关闭我正在写入差异的文件。

【讨论】:

  • 这意味着正在写入的内容很小,小于默认缓冲区大小......
  • 所以你的意思是说如果我写了很多差异(足够填满缓冲区),即使我没有关闭文件,它也会写出缓冲区来重置它?跨度>
  • 我认为如果内容更大,文件不会是零大小,但它可能是不完整的。教训:始终关闭您打开的文件句柄。
【解决方案2】:
set status [catch {exec diff $file1 $file2} result]

“状态”不是diff的退出状态,是catch的退出状态。不一样。获取diff的退出状态:

if {$status == 1} {
    set err_type [lindex $::errorCode 0]
    if {$err_type eq "NONE"} {
        # diff exited with exit status 0 but printed something
        # to stderr
    } elseif {$err_type eq "CHILDSTATUS"} {
        # diff returned non-zero status
        set diff_exit_status [lindex $::errorCode end]
        if {$diff_exit_status == 1} {
            # diff results in $result
        } else {
            # diff had some kind of trouble
        }
    }
}

【讨论】:

  • 我收到错误:cant read "errorCode": no such variable。但是,这会解决 file.txt.gz 文件为空的初始问题吗?
  • 啊,这是在一个过程中。使用$::errorCode获取全局变量。
  • 您说创建差异的proc正在工作,并且运行compress proc后压缩文件为空。您认为错误最有可能出现在哪里?
  • 是的,但是正如我的问题中提到的,当我运行 diff proc 而不运行 compress proc 时,它可以工作!当我运行 diff proc 并注释掉 compress proc 时,我的 diff 文件具有预期的差异
  • 好的。然后你运行 compress proc 并看到“file.txt.gz”是空的。你认为哪个proc对此负责?我认为这是压缩过程。也许你应该向我们展示那个 proc 的作用,嗯?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-01
相关资源
最近更新 更多