Chinaunix首页 | 论坛 | 博客
  • 博客访问: 613974
  • 博文数量: 486
  • 博客积分: 10125
  • 博客等级: 上将
  • 技术积分: 5842
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-27 18:34
文章分类

全部博文(486)

文章存档

2011年(52)

2010年(107)

2009年(289)

2008年(38)

我的朋友

分类: LINUX

2009-05-23 22:23:12

tcl文件如何从shell命令行读取参数[好东西,原文好点]
2008-05-18 23:05

Tcl
Command Line Arguments





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, , 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 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.

阅读(1047) | 评论(0) | 转发(0) |
0

上一篇:带参数的shell

下一篇:shell学习

给主人留下些什么吧!~~