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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24376421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:53:49  来源:igfitidea点击:

Passing arguments from command line into a tcl script

bashtcl

提问by pypep278

I have a TCL script that has many parameters defined as arguments. I intend to create a job file where I can execute the .tcl script with different combinations of the parameters without manual intervention.

我有一个 TCL 脚本,它有许多定义为参数的参数。我打算创建一个作业文件,我可以在其中执行具有不同参数组合的 .tcl 脚本,而无需手动干预。

What I mean is the following:

我的意思是:

Job File (run.sh):

作业文件(run.sh):

./main.tcl arg1 arg2 arg3 arg4 
./main.tcl arg1 arg3 arg5

Now I want to be able to pass the command line argument array "argv" for each run mentioned in run.sh as an array into the main.tcl script so that the options are set accordingly within the script prior to execution.

现在,我希望能够将 run.sh 中提到的每次运行的命令行参数数组“argv”作为数组传递到 main.tcl 脚本中,以便在执行之前在脚本中相应地设置选项。

Is there a way to link the .sh script and the .tcl script?

有没有办法链接 .sh 脚本和 .tcl 脚本?

回答by tinlyx

Per online document here:

根据此处的在线文档

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]]
    }

可以将数字传入并由脚本使用的方法如下。

argc argv argv0

所有 Tcl 脚本都可以访问三个预定义的变量。$argc - 传递给脚本的参数项数。$argv - 参数列表。$argv0 - 脚本的名称。

要使用这些参数,可以按如下方式重新编写脚本。

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]]
    }

回答by Raj

In addition to the above answer, I found the following helpful:

除了上述答案外,我发现以下内容很有帮助:

For running within QuestaSim/ModelSim/Quartus I followed the following resource: http://www.alteraforum.com/forum/showthread.php?t=3034

为了在 QuestaSim/ModelSim/Quartus 中运行,我遵循了以下资源:http: //www.alteraforum.com/forum/showthread.php?t=3034

Typically we can run .tcl scripts with the sourcecommand, but this doesn't inherently process arguments like base .tcl scripts can use normally. So by using a process, you can achieve the same thing.

通常我们可以使用该source命令运行 .tcl 脚本,但这本身并不像基本 .tcl 脚本可以正常使用的那样处理参数。因此,通过使用流程,您可以实现相同的目标。