2

我正在尝试创建一个 tcl proc,它传递一个 shell 命令作为参数,然后打开一个临时文件并将格式化的字符串写入临时文件,然后在后台运行 shell 命令并将输出存储到临时文件也是。

在后台运行命令,以便之后可以立即调用 proc,并将另一个 arg 传递给它,写入另一个文件。因此,运行一百个这样的命令所花费的时间不应该像串行运行它们那样长。多个临时文件最终可以连接成一个文件。

这是我正在尝试做的伪代码。

proc runthis { args }  
{ 
    set date_str [ exec date {+%Y%m%d-%H%M%S} ]
    set tempFile ${date_str}.txt
    set output [ open $tempFile a+ ]
    set command [concat exec $args]
    puts $output "### Running $args ... ###"   

    << Run the command in background and store output to tempFile >>
}

但是我如何确保任务的后台处理正确完成?需要做些什么来确保正确关闭多个临时文件?

欢迎任何帮助。我是 tcl 的新手,我想解决这个问题。我读过关于在 tcl 中使用线程,但我正在使用不支持线程的旧版本 tcl。

4

1 回答 1

1

怎么样:

proc runthis { args }  { 
    set date_str [clock format [clock seconds] -format {+%Y%m%d-%H%M%S}]
    set tempFile ${date_str}.txt
    set output [ open $tempFile a+ ]
    puts $output "### Running $args ... ###"   
    close $output

    exec {*}$args >> $tempFile &
}

http://tcl.tk/man/tcl8.5/TclCmd/exec.htm

由于您似乎有一个较旧的 Tcl,请替换

    exec {*}$args >> $tempFile &

    eval exec [linsert $args 0 exec] >> $tempFile &
于 2012-12-17T11:54:08.810 回答