如果您不確定要傳遞的參數的數量,那麼可以使用eval
或參數擴展運算符{*}
。
如果您Tcl
的版本是8.5或以上,
spawn <program-name> {*}$argv
否則,
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
[email protected]:~/stackoverflow$ tclsh cmdlinearg STACK OVER FLOW
,這將給輸出
0 : STACK
1 : OVER
2 : FLOW
THE END
現在執行下面的命令,讓我們寫一個詳細計劃,將與任意數量的命令沿產卵這個節目線參數。
MyProgram.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
......問題在哪裏? –