TCL:exec 中的字符串替换,但不作为单个参数

问题描述 投票:0回答:1

在 TCL 脚本中,我想执行如下命令:

set result [exec ${exe} ${options} ${prog}]

其中

options
类似于
set options "-a -b -c"
set options ""
。但这不起作用,因为执行的内容就像

${exe} "-a -b -c" ${prog}
${exe} "" ${prog}

并且不喜欢

${exe} -a -b -c ${prog}
${exe}  ${prog}

如何扩展字符串,使其不是单个参数?文档说

[]
替换命令的结果,但是

set result [exec ${exe} [puts ${options}] ${prog}]

没有做文档所承诺的事情...

tcl
1个回答
0
投票

有两种选择,具体取决于您想要的。

经典的 Tcl 技术是使用参数扩展语法:

set result [exec $exe {*}$options $prog]

但是如果你想使用 shell 语法,你可以对此做一些变体:

set result [exec sh -c "exec '$exe' $options '$prog'"]

(假设

$exe
$prog
不包含单引号。)

© www.soinside.com 2019 - 2024. All rights reserved.