如何通过 Linux 框中的 tcl 脚本压缩多个文件?

How to zip multiple files through tcl script in Linux box?

我在 tcl 中有一组代码,我试图在其中实现压缩文件,但出现以下错误

zip warning: name not matched: a_1.txt a_2.txt a_3.txt a_4.txt

另一方面,我在命令提示符下做同样的事情我能够成功执行。

#!/usr/local/bin/tclsh

set outdir /usr/test/
set out_files abc.10X
array set g_config { ZIP /usr/bin/zip }
set files "a_1.txt a_2.txt a_3.txt a_4.txt"

foreach inp_file $files {
    append zipfiles "$inp_file "
} 
exec $g_config(ZIP) $outdir$out_files zipfiles 

Tcl 非常关心单词之间的界限,除非被要求否则不会拆分。这很好,因为它意味着像带有 spaces 的文件名之类的东西不会混淆它,但在这种情况下它会给你带来一些问题。

要让它拆分列表,请在从变量中读取单词之前加上 {*}:

exec $g_config(ZIP) $outdir$out_files {*}$files

这是而不是

exec $g_config(ZIP) $outdir$out_files $files
# Won't work; uses "strange" filename

或者这个:

exec $g_config(ZIP) $outdir$out_files zipfiles
# Won't work; uses filename that is the literal "zipfiles"
# You have to use $ when you want to read from a variable and pass the value to a command.

非常 旧版本的 Tcl,其中 {*} 不起作用?升级到 8.5 或 8.6!或者至少使用这个:

eval {exec $g_config(ZIP) $outdir$out_files} $files

(如果你把 space 放在 outdir 中,你需要大括号...)