【发布时间】:2020-05-19 17:52:28
【问题描述】:
我正在修改下面的代码,但我不知道它是如何工作的 - 欢迎启发。问题是其中有一个 proc (cygwin_prefix),它旨在通过
- 不修改文件名,或
- 在文件名前添加一个字符串
问题是 proc 没有返回任何内容,但脚本仍然神奇地工作。如何?具体来说,set command [cygwin_prefix filter_g] 行实际上是如何正确设置command 的?
对于背景,脚本只执行 filter_g < foo.txt > foo.txt.temp。然而,从历史上看(这似乎不再是这种情况)这在 Cygwin 上不起作用,所以它改为运行 /usr/bin/env tclsh filter_g < foo.txt > foo.txt.temp。所示脚本在 Linux (Tcl 8.5) 和 Cygwin (Tcl 8.6) 上都“有效”。
谢谢。
#!/usr/bin/env tclsh
proc cygwin_prefix { file } {
global cygwin
if {$cygwin} {
set status [catch { set fpath [eval exec which $file] } result ]
if { $status != 0 } {
puts "which error: '$result'"
exit 1
}
set file "/usr/bin/env tclsh $fpath"
}
set file
}
set cygwin 1
set filein foo.txt
set command [cygwin_prefix filter_g]
set command "$command < $filein > $filein.temp"
set status [catch { eval exec $command } result ]
if { $status != 0 } {
puts "filter error: '$result'"
exit 1
}
exit 0
【问题讨论】:
-
相关搜索:"tcl proc return".
-
@user2864740 - 谢谢 - 然后解释了最后的
set。如果你想发布,我会接受。
标签: tcl