tcl文件如何从shell命令行读取参数

 我来答
yexicheng
2014-11-04 · TA获得超过1.2万个赞
知道大有可为答主
回答量:8857
采纳率:69%
帮助的人:6983万
展开全部
Introduction
Although Tcl scripts/procedures can be controlled via graphical user interfaces (GUI's) that have been implemented with the Tk toolkit, it is often easier to use traditional command line techniques. Another tutorial, using the command line, gave a simple example of invoking a Tcl script from the command line.

This tutorial demonstrates how, at the time a script is invoked, data can be passed to the script from the command line.

arguments
Items of data passed to a script from the command line are known as arguments. For example, take the simple script presented in using the command line tutorial.
puts [expr 3 + 2]

Lets assume the script has been saved as add.tcl and that the present working directory of the shell window in which we are working matches the directory in which the script has been saved. We know the script can be run using the following command,
tclsh add.tcl

Of course the output is always the same because the values being added are hard-coded into the script. The script would be more useful if we could pass values to the script from the command line.
tclsh add.tcl 23 15

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]]
}
The lindex command returns the first and second items from the list of arguments entered at the command line. Items in a list are counted from zero.
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式