我有一个期望例程,它需要生成一个进程并将我传递给expect例程的命令行参数传递给生成的进程。
我的期望例程有以下行
spawn myProcess $argv
当我调用我的期望例程时,我从命令行调用它,如下所示
expect myRoutine <arg1> <arg2> <arg3>
当我这样做时,期望抛出以下错误
Can't open input file <arg1> <arg2> <arg3> for reading
但是如果我改变我的期望程序如下
spawn myProcess [lindex $argv 0] [lindex $argv 1] [lindex $argv 2]
生成myProcess没有任何错误。但是这对我没有用,因为我不能保证我总会有三个参数传递给expect例程。
如何将命令行参数从unix shell的命令行传递给expect中的生成进程?
答案 0 :(得分:4)
如果您不确定将要传递的参数数量,那么您可以使用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
中执行以下命令dinesh@PC:~/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