File io 将变量的文本值写入文件

File io 将变量的文本值写入文件,file-io,tcl,File Io,Tcl,我有一段令人畏惧的代码: exec zip -r $FULLPATH error.log [ append datetime $RECENT ".bwdb" ] [ append txt $testName ".txt" ] [ append lancap $testName "-lan.cap" ] \ [ append lanmcap $testName "-lan-m.cap" ] [ append wancap $testName "-wan.cap" ] [ appe

我有一段令人畏惧的代码:

exec zip -r $FULLPATH error.log [ append datetime $RECENT ".bwdb" ] [ append txt $testName  ".txt" ] [ append lancap $testName "-lan.cap" ] \
        [ append lanmcap $testName "-lan-m.cap" ] [ append wancap $testName "-wan.cap" ] [ append wanmcap $testName "-wan-m.cap" ] [ append conf $confFile ".conf" ] \
        start.txt start-lan.cap start-lan-m.cap start-wan.cap start-wan-m.cap [ append comments "comments-" $RECENT ".bwc" ]
实际上就是把一堆文件压缩在一起。我正在更改生成zip文件的方式(更准确地说,我在生成zip文件时正在更改)。我决定这样做的方式是将每个命令保存到一个文本文件中,然后在需要创建它们时,只需遍历文件中的每个命令。问题是我有一堆变量需要转换成文字形式

有人知道我如何将这个命令转换成文本值并存储到文本文件中吗

编辑:我也愿意听取解决同一问题的其他建议,和/或我建议的方式的任何利弊

编辑2:谢谢你的帮助,我选择这样做:

set data123 "exec zip -r $FULLPATH error.log [ append datetime $RECENT \".bwdb\" ] [ append txt $testName  \".txt\" ] [ append lancap $testName \"-lan.cap\" ] \
        [ append lanmcap $testName \"-lan-m.cap\" ] [ append wancap $testName \"-wan.cap\" ] [ append wanmcap $testName \"-wan-m.cap\" ] [ append conf $confFile \".conf\" ] \
        start.txt start-lan.cap start-lan-m.cap start-wan.cap start-wan-m.cap [ append comments \"comments-\" $RECENT \".bwc\" ]"

set datafile123 "datafile123.txt"
set fileId123 [ open $datafile123 "w" ]
puts $fileId123 $data123
close $fileId123
但是,当我查看结果文件时,我看到:

exec zip -r /home/IOL/TR069_Certification/Results/TEST_Round99_GetRPCMethods_CDR1_20130410172812 error.log 20130410172812.bwdb20130410172812".bwdb" GetRPCMethods.txtGetRPCMethods".txt" GetRPCMethods-lan.capGetRPCMethods"-lan.cap"  GetRPCMethods-lan-m.capGetRPCMethods"-lan-m.cap" GetRPCMethods-wan.capGetRPCMethods"-wan.cap" GetRPCMethods-wan-m.capGetRPCMethods"-wan-m.cap" IOL.confIOL".conf"  start.txt start-lan.cap start-lan-m.cap start-wan.cap start-wan-m.cap comments-20130410172812.bwc"comments-"20130410172812".bwc"

这似乎是在每个附加的末尾生成连接字符串的第二个副本,我不知道为什么。

我基本上只是按照您的建议:将文本值写入文本文件。 实现这一点的一种方法很简单:

set s ""
append s \
  error.log \n \
  $datetime $RECENT .bwdb \n \
  $txt $testName .txt \n \
  $lancap $testName -lan.cap \n \
  $lanmcap $testName -lan-m.cap \n \
  $wancap $testName -wan.cap \n \
  $wanmcap $testName -wan-m.cap \n \
  $conf $confFile .conf \n \
  start.txt \n start-lan.cap \n \
  start-lan-m.cap \n start-wan.cap \n start-wan-m.cap \n \
  $comments comments- $RECENT .bwc
set f [open /path/to/file w]
puts $f $s
close $f
这将创建一个文本文件,每行一个文件名

另一个更直接的返工可能如下所示:

set files [list]
lappend files \
  error.log \
  [ append datetime $RECENT ".bwdb" ] \
  [ append txt $testName  ".txt" ] \
  [ append lancap $testName "-lan.cap" ] \
  [ append lanmcap $testName "-lan-m.cap" ] \
  [ append wancap $testName "-wan.cap" ] \
  [ append wanmcap $testName "-wan-m.cap" ] \
  [ append conf $confFile ".conf" ] \
  start.txt start-lan.cap start-lan-m.cap start-wan.cap start-wan-m.cap \
  [ append comments "comments-" $RECENT ".bwc" ]
set f [open /path/to/file w]
puts $f [join $files \n]
close $f
set files [list]
lappend files \
  error.log \
  ${datetime}${RECENT}.bwdb \
  ${txt}${testName}.txt \
  ${lancap}${testName}-lan.cap \
  ${lanmcap}${testName}-lan-m.cap \
  ${wancap}${testName}-wan.cap \
  ${wanmcap}${testName}-wan-m.cap \
  ${conf}${confFile}.conf \
  start.txt start-lan.cap start-lan-m.cap start-wan.cap start-wan-m.cap \
  ${comments}comments-${RECENT}.bwc
set f [open /path/to/file w]
puts $f [join $files \n]
close $f
我对
append
的这种用法表示怀疑,尽管在这里,这个命令似乎只是用于它的副作用
append
获取其倒数第二个参数的字符串,并将其附加到变量中的字符串,该变量的名称作为其第一个参数传递,并返回结果字符串。我不喜欢在您最初的示例(以及我的第二个示例)中,
append
似乎只用于返回值

因此,可能更惯用的方法是使用Tcl的简单字符串连接方式,如下所示:

set files [list]
lappend files \
  error.log \
  [ append datetime $RECENT ".bwdb" ] \
  [ append txt $testName  ".txt" ] \
  [ append lancap $testName "-lan.cap" ] \
  [ append lanmcap $testName "-lan-m.cap" ] \
  [ append wancap $testName "-wan.cap" ] \
  [ append wanmcap $testName "-wan-m.cap" ] \
  [ append conf $confFile ".conf" ] \
  start.txt start-lan.cap start-lan-m.cap start-wan.cap start-wan-m.cap \
  [ append comments "comments-" $RECENT ".bwc" ]
set f [open /path/to/file w]
puts $f [join $files \n]
close $f
set files [list]
lappend files \
  error.log \
  ${datetime}${RECENT}.bwdb \
  ${txt}${testName}.txt \
  ${lancap}${testName}-lan.cap \
  ${lanmcap}${testName}-lan-m.cap \
  ${wancap}${testName}-wan.cap \
  ${wanmcap}${testName}-wan-m.cap \
  ${conf}${confFile}.conf \
  start.txt start-lan.cap start-lan-m.cap start-wan.cap start-wan-m.cap \
  ${comments}comments-${RECENT}.bwc
set f [open /path/to/file w]
puts $f [join $files \n]
close $f

为什么不是简单的字符串呢?您知道一个
字符串可以跨越多行吗

set f [open /p/t/f w]
puts $f "error.log
$datetume$RECENT.bwdb
$txt$testName.txt
$lancap$testName-lan.cap
$lanmcap$testName-lan-m.cap
$wancap$testName-wan.cap
$wanmcap$testName-$wan.cap
$conf$confFile.conf
start.txt
start-lan.cap
start-lan-m.cap
start-wan.cap
start-wan-m.cap
${comments}comments-$RECENT.bwc"
close $f
编辑:不应缩进后续行,否则此缩进将显示在结果文件中。

我个人通常使用一个模板文件,并使用
subst
替换变量。

非常感谢关于TCL的提示。我在工作中被甩到一个TCL项目中,我一直在自学,所以这些东西不时出现。