我正在尝试创建一个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。
怎么样:
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 &