从命令行传递参数到tcl脚本

9
我有一个TCL脚本,其中许多参数被定义为参数。我打算创建一个作业文件,在其中可以执行.tcl脚本,使用不同的参数组合而无需手动干预。
我的意思是以下内容:
作业文件(run.sh):
./main.tcl arg1 arg2 arg3 arg4 
./main.tcl arg1 arg3 arg5

现在我想要能够将run.sh中提到的每个运行的命令行参数数组“argv”作为一个数组传递给main.tcl脚本,以便在执行之前在脚本内设置选项。
有没有一种方法可以将.sh脚本和.tcl脚本链接起来?

在您的shell脚本中,您可以将所有参数列表称为$@。因此只需说main.tcl $@ - John C
@JohnC - 感谢回复!好的,如果我在run.sh中执行./main.tcl $@,那么我如何在main.tcl中检索参数?使用argv吗? - pypep278
我认为下面的答案已经为您解决了这个问题。 - John C
这个链接可以解释它并且给你一个具体的例子。 - user1934428
4个回答

9

根据在线文档

The method by which numbers can be passed into, and used by a script, is as follows.

argc argv argv0

All Tcl scripts have access to three predefined variables. $argc - number items of arguments passed to a script. $argv - list of the arguments. $argv0 - name of the script.

To use the arguments, the script could be re-written as follows.

if { $argc != 2 } {
    puts "The add.tcl script requires two numbers to be inputed."
    puts "For example, tclsh add.tcl 2 5".
    puts "Please try again."
} else {
    puts [expr [lindex $argv 0] + [lindex $argv 1]]
    }

这个链接已经失效了。 - Brian
2
请注意,以这种方式编写的代码存在命令注入漏洞。例如,尝试使用tclsh that-script '[exec sh -c {id>/dev/tty; echo 1}]' 2。更好的方法是使用puts [expr {[lindex $argv 0] + [lindex $argv 1]}] - Stephane Chazelas
1
请返回已翻译的文本:链接为http://www.fundza.com/tcl/script_shell/arguments/index.html,这是有关编程的内容。 - nillu
为了改进这个例子,如果找不到参数,有一个退出脚本的命令并返回错误代码会很有帮助。 - undefined

1
为了演示如何将命令行参数传递到TCL脚本中:
创建一个名为args.tcl的文本文件,其中包含以下命令:
# If no command line arguments are passed perform no actions
if {$argc > 0} {
# argv0 conatins the filename of the script
puts "The name of the script is: $argv0"
# argc contains the count of the arguments passed
puts "Total count of arguments passed is: $argc"
# argv contains a list of the arguments
puts "The arguments passed are: $argv"
# Using the List Index of argv print a specific argument
puts "The first argument passed was [lindex $argv 0]"
}

在创建文件后,使用以下命令行调用脚本:

> Tclsh85 args.Tcl ONE 2 3
The name of the script is: args.Tcl
Total count of arguments passed is: 3
The arguments passed are: ONE 2 3
The first argument passed was ONE
>

参考资料:Tcl/Tk 8.5 编程食谱


1
除了上面的回答外,我还发现以下内容很有帮助:
对于在 QuestaSim/ModelSim/Quartus 中运行,我遵循了以下资源:http://www.alteraforum.com/forum/showthread.php?t=3034 通常我们可以使用 source 命令运行 .tcl 脚本,但这并不会像基本的 .tcl 脚本一样自然地处理参数。因此,通过使用进程,您可以实现相同的效果。

1

我发现,解析参数(其中有用的始终作为列表传递到全局argv变量中,该列表的长度在全局argc变量中; C程序员将认识这些名称)最容易像这样:

switch $argc {
    2 {
        # Split the arguments and set the default for the optional
        lassign $argv firstArg secondArg
        set thirdArg "default value"
    }
    3 {
        # Split all the arguments
        lassign $argv firstArg secondArg thirdArg
    }
    default {
        # Oh no! Give the user an error message and stop
        error "wrong # args: should be \"$argv0 firstArg secondArg ?thirdArg?\""
    }
}

全局变量argv0是脚本的名称。正确来说,它应该是argv/argc中的一个参数(可能还包括Tcl解释器的名称),但几乎没有人以同样的方式使用它,所以它被拆分出来供您使用。正如我所说,有用的参数在argv中。
您还可以使用任何其他列表处理命令与argv一起使用;例如,foreach非常适合处理文件名序列。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接