如何在 tcl 中向 exec 添加可变数量的参数?
我已经与 TCL 合作有一段时间了,我花了很长时间尝试执行以下操作(这似乎很容易,我认为应该如此,但我无法正确执行):
我需要执行一个通过 tcl 脚本的外部程序。为此,我使用 exec 命令。为了使用这个外部程序,我需要输入不同数量的文件。如果我直接从 cmd 窗口调用该程序,它会类似于:
C:\>myprogram -i file1 -i file2 -i file3 (etc., etc.)
但是,当尝试通过 tcl 以动态/可变方式实现此程序时,我遇到了麻烦。我这样做的方法是将我需要的所有“-i filex
”存储在某个变量 myvar 中(在循环中完成),然后将其作为参数传递给 exec命令。它看起来像:
exec myprogram $myvar
这样做显然会产生一些问题,因为这个 myprogram 无法“看到”myvar。我猜测存在某种隐藏的终止符或不同类型参数的某些冲突,使得最终 exec 命令“看到”只有我的程序。
所以,我的问题是,有谁知道如何将变量参数插入到对 exec 的调用中?
I've been working with TCL for some time now, and I have spent a long time trying to do the following (it seems easy and I think it should be, but I can't get it right):
I need to execute an external program by means of a tcl script. For that, I use the exec
command. For using this external program, I need to input a variable amount of files. If I called this program straight from a cmd window, it would be something like:
C:\>myprogram -i file1 -i file2 -i file3 (etc., etc.)
However, when trying to implement this in a dynamic/variable way through tcl I get into trouble. The way I do it is by storing in some variable myvar all the "-i filex
" I need (done in a loop), and then pass that as a parameter to the exec
command. It would look something like:
exec myprogram $myvar
Doing that apparently creates some issues, because this myprogram fails to "see" myvar. I'm guessing that there is some sort of hidden terminator or some clash of different types of arguments that makes that in the end the exec command "sees" only myprogram.
So, my question is, does anyone know how to insert variable arguments into a call to exec
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用{*}或eval。例如,请参阅此问题。
You can use {*} or eval. See this question for example.
专门针对您的情况:
Tcl 8.5(及更高版本):
Tcl 8.4(及之前版本):
是的,旧版本很丑陋(或不明显,或两者兼而有之)。因此,人们倾向于这样写:
但这比预期的要慢(好吧,在运行外部程序时不太相关!)并且当
$myvar
不是规范格式时会存在危险由于eval
的工作方式而列出。它曾经甚至能吸引经验丰富的 Tcl 程序员,这就是我们在 8.5 中引入新语法的原因,该语法被指定为无意外且非常短。Specializing for your case:
Tcl 8.5 (and later):
Tcl 8.4 (and before):
That's right, the old version is ugly (or non-obvious, or both). Because of that, people tended to write this instead:
but that was slower than expected (OK, not so relevant when running an external program!) and has hazards when
$myvar
isn't a canonically-formatted list due to the way thateval
works. It used to catch out even experienced Tcl programmers, and that's why we introduced new syntax in 8.5, which is specified to be surprise-free and is pretty short too.