如果您不确定要传递的参数数量,那么您可以使用eval
或参数扩展运算符{*}
.
If your Tcl
的版本是8.5以上,
spawn <program-name> {*}$argv
Else,
eval spawn <program-name> $argv
让我们考虑以下问题Tcl
程序
cmdLinearG.tcl
#!/usr/bin/tclsh
set count 0;
if { $argc == 0 } {
puts "No args passed :("
exit 1
}
foreach arg $argv {
puts "$count : $arg"
incr count
}
puts "THE END"
该程序将接收任意数量的命令行参数。要运行该程序,我们在 shell 中执行以下命令
dinesh@PC:~/stackoverflow$ tclsh cmdlinearg STACK OVER FLOW
这将给出输出
0 : STACK
1 : OVER
2 : FLOW
THE END
现在,让我们再编写一个程序,该程序将生成该程序以及任意数量的命令行参数。
我的程序.tcl
#!/usr/bin/expect
# If your Tcl version is 8.4 or below
eval spawn tclsh $argv
expect eof
# If your Tcl version is 8.5 or above
spawn tclsh {*}$argv
expect eof
如果假设您想将程序名称本身作为参数传递,那也是可能的。
# Taking the first command line arg as the program name and
# using rest of the args to the program
eval spawn [lindex argv 0] [ lrange $argv 0 end ]
expect eof