2012-12-21 49 views
1

我试图将文件夹中的所有临时文件连接到单个文本文件中。但我一直在错误运行:将临时文件连接到tcl中的单个文件

if { [catch { exec cat /tmp/new_temp/* >> /tmp/full_temp.txt } msg] } 

错误消息:

-cat: /tmp/new_temp/*: No such file or directory 

如果我尝试在tclsh的同样的事情(不捕,和exec)它的工作原理

回答

5

为什么这么可怕的做法?使用Tcl本身连接​​这些文件:

set out [open /tmp/full_temp.txt w] 
fconfigure $out -translation binary 
foreach fname [glob -nocomplain -type f "/tmp/new_temp/*"] { 
    set in [open $fname] 
    fconfigure $in -translation binary 
    fcopy $in $out 
    close $in 
} 
close $out 
+0

+1 fcopy会非常快。 –

+0

glob应该在这里做的参数是什么?我收到一个错误(/坏参数“-types”:-f) 它似乎工作正常,如果我删除“-type f” – egorulz

+0

@egorulz,[它只选择文件](http: //www.tcl.tk/man/tcl8.4/TclCmd/glob.htm#M10)。否则,你需要在每个匹配条目上执行'file stat',看看它是否真的是一个文件,而不是一个目录或套接字或fifo。这是必需的,因为试图对一个目录执行fcopy操作会失败并出现错误,并尝试使用套接字或fifo执行此操作可能只是尝试从它们读取数据,而这些数据本来就是错误的)。请注意,您使用'cat'的原始尝试很容易出现同样的问题。 – kostix

2

,因为TCL还是不是shell,它不会自动扩展glob模式。尽量

if { [catch {exec sh -c {cat /tmp/new_temp/* >> /tmp/full_temp.txt}} msg] } 

要获得的Tcl做文件名扩展,你需要的glob命令

set code [catch [list exec cat {*}[glob /tmp/new_temp/*] >> /tmp/full_temp.txt] msg] 
if {$code != 0} { 
    # handle error 
} 
+0

第二个示例是错误的,因为它错过了'cat'本身。我还会强调,这种情况是*罕见的*其中'glob'的默认行为 - 如果没有匹配的文件就炸掉 - 实际上是正确的,否则'cat'会试图从流程中读取'stdin'。我的意思是,通常将'-nocomplain'传递给'glob'是可取的,但这种情况是不同的。 – kostix

+0

哎呀错过了。我再拍一脚猫 –