如何在TCL中解释字符串列表中的“”?

如何在TCL中解释字符串列表中的“”?,tcl,Tcl,我想构造一个复杂的变量,让我们看看: set entry_arg [lindex $argv 1] set command_to_launch {extrernal_command_tool -option "set var $entry_arg" -other_option} exec $command_to_lauch 命令启动了,没问题 但是,问题是该工具获取$entry_arg并崩溃。。。这是由于不允许tcl解释de变量的 set command_to_launch {extrerna

我想构造一个复杂的变量,让我们看看:

set entry_arg [lindex $argv 1]
set command_to_launch {extrernal_command_tool -option "set var $entry_arg" -other_option}
exec $command_to_lauch
命令启动了,没问题

但是,问题是该工具获取$entry_arg并崩溃。。。这是由于不允许tcl解释de变量的

set command_to_launch {extrernal_command_tool -option "set var [lindex $argv 1]"  -other_option}
我也有同样的问题


如何解决此问题?

您应该使用该命令构造命令\u to \u launch的内容

您可以使用以下任一选项运行此操作:

exec {*}$command_to_launch           ; # For 8.5 or 8.6
eval exec $command_to_launch         ; # For 8.4 or before
当然,选择正确的语言版本。

问题不是,而是{}。尝试:

您需要学习插值、引用和大括号,如下所示:

另外,我怀疑您想要使用[lindex$argv 0]-TCL使用变量argv0作为程序名,参数从索引0开始,这与C和其他语言不同

编辑: 如果您真的想要一个引用的参数,比如set var xxx,那么您可能是这个意思:


另一种方法是使用该语言的语法特性构造Bourne shell脚本,然后使用exec/bin/sh-c$theScriptFile执行它;由于用户的期望,这有时是最好的选择。好吧,它更好,但在我的-other_选项中,我有一些[]!所以评估人员现在会为此出错!您的方法还将转换为{},并使外部工具崩溃@嘿,我明白了,你的无能很深。列表中的单词在Windows上通过适当的引号或Unix上的直接分隔成为子流程的独立参数,因此任何括号或大括号都是数据中的参数。你的抱怨表明所发生的一切都不是你告诉我们的;在这一点上,我至少真的不想再尝试修复它了。@heyhey,一定要阅读以理解为什么应该使用list来构造eval脚本。这个方法不是最健壮的TCL,它有点欺骗,因为它只在命令\u to \u launch是一个简单的字符串时才起作用。在一般情况下,尤其是在将eval用于其他目的时,使用另一个答案中所示的列表是正确而稳健的方法。尽管您已经了解了它如何与引号和$/[/.@kostix下面的链接进行交互,但该方法确保所有内容都被正确解析。
exec {*}$command_to_launch           ; # For 8.5 or 8.6
eval exec $command_to_launch         ; # For 8.4 or before
set entry_arg [lindex $argv 0]
set command_to_launch "extrernal_command_tool -option $entry_arg -other_option"
set output [eval exec $command_to_lauch]
    set command_to_launch "extrernal_command_tool -option \"set var $entry_arg\" -other_option"