在 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 技术是使用参数扩展语法:
set result [exec $exe {*}$options $prog]
但是如果你想使用 shell 语法,你可以对此做一些变体:
set result [exec sh -c "exec '$exe' $options '$prog'"]
(假设
$exe
和$prog
不包含单引号。)