在期望中使用argc和argv解析命令行

时间:2022-04-18 22:44:28

I have an expect routine, which needs to spawn a process and pass the command line arguments I passed to the expect routine to the spawned process.

我有一个期望例程,它需要生成一个进程并将我传递给expect例程的命令行参数传递给生成的进程。

My expect routine has the following line

我期望的例程有以下几行

spawn myProcess $argv

and when I call my expect routine I call it from the command line as follows

当我调用我的期望例程时,我从命令行调用它,如下所示

expect myRoutine <arg1> <arg2> <arg3>

When I do this, expect throws the following error

当我这样做时,期望抛出以下错误

Can't open input file <arg1> <arg2> <arg3> for reading

However if I change my expect routine as follows

但是,如果我改变我的期望例程如下

spawn myProcess [lindex $argv 0] [lindex $argv 1] [lindex $argv 2]

myProcess is spawned without any errors. However this is not useful to me, as I cannot guarantee that I would always have three arguments passed to the expect routine.

生成myProcess没有任何错误。但是这对我没用,因为我不能保证我总会有三个参数传递给expect例程。

How do I pass command line arguments from the command line of a unix shell to the spawned process in expect?

如何将命令行参数从unix shell的命令行传递给expect中的生成进程?

1 个解决方案

#1


If you are not sure about the number of arguments which is going to be passed, then, you can make use of eval or argument expansion operator {*}.

如果您不确定将要传递的参数数量,那么您可以使用eval或参数扩展运算符{*}。

If your Tcl's version is 8.5 or above,

如果您的Tcl版本是8.5或更高,

spawn <program-name> {*}$argv

Else,

eval spawn <program-name> $argv

Lets consider the following Tcl program

让我们考虑以下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"

This program will receive any number of command line arguments. To run this program, we execute the following command in the shell

该程序将接收任意数量的命令行参数。要运行此程序,我们在shell中执行以下命令

dinesh@PC:~/*$ tclsh cmdlinearg STACK OVER FLOW

which will give output as

这将输出为

0 : STACK
1 : OVER
2 : FLOW
THE END

Now, lets write one more program which will spawn this program along with any number of command line arguments.

现在,让我们再写一个程序,它将生成这个程序以及任意数量的命令行参数。

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

If suppose, you want to pass your program name itself as an argument, that is also possible.

如果假设,您希望将程序名称本身作为参数传递,那也是可能的。

# 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

#1


If you are not sure about the number of arguments which is going to be passed, then, you can make use of eval or argument expansion operator {*}.

如果您不确定将要传递的参数数量,那么您可以使用eval或参数扩展运算符{*}。

If your Tcl's version is 8.5 or above,

如果您的Tcl版本是8.5或更高,

spawn <program-name> {*}$argv

Else,

eval spawn <program-name> $argv

Lets consider the following Tcl program

让我们考虑以下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"

This program will receive any number of command line arguments. To run this program, we execute the following command in the shell

该程序将接收任意数量的命令行参数。要运行此程序,我们在shell中执行以下命令

dinesh@PC:~/*$ tclsh cmdlinearg STACK OVER FLOW

which will give output as

这将输出为

0 : STACK
1 : OVER
2 : FLOW
THE END

Now, lets write one more program which will spawn this program along with any number of command line arguments.

现在,让我们再写一个程序,它将生成这个程序以及任意数量的命令行参数。

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

If suppose, you want to pass your program name itself as an argument, that is also possible.

如果假设,您希望将程序名称本身作为参数传递,那也是可能的。

# 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