TCL command language interfaceFerdi
Franceschini
2006-08-17 14:01
Common commands & exclusions
From the PSI SANS documentation by Dr. Joachim Kohlbrecher and Dr. Mark Könnecke with
slight modifications.
The macro language implemented in the SICS server is John Ousterhout Tool Command
Language TCL . Tcl has control constructs,
variables of its own, loop constructs, associative arrays and procedures. Tcl is well
documented by several books, online
tutorials and manuals. All SICS commands are available in the macro language.
Some potentially harmful Tcl commands have been deleted from the standard Tcl
interpreter. These are:
exec
source
puts
vwait
exit
gets
socket
Below only a small subset of the most important Tcl commands like assigning variables,
evaluating expressions, control and loop constructs are described. For complete
description of Tcl commands have a look on the manual pages or on one of the many
books about Tcl/Tk.
set varName value
set arrName(index) value
Set/get scalar variables or array elements. Arrays in Tcl are actually
associative arrays, this means that their indices are not restricted to
integers. The following examples demonstrate setting a scalar variable and
a couple of array elements. Note the third array example which shows that
the same array can have mixed indices (the number 1 and 'one') as well as
mixed data types (the number 10 and 'ten') in the same array.
set a 3
set arr(1) 10
set arr(one) ten
expr arg arg arg
Concatenates arg’s (adding separator spaces between them), evaluates the
result as a Tcl expression, and returns the value. The operators permitted
in Tcl expressions are a subset of the operators permitted in C expressions,
and they have the same meaning and precedence as the corresponding C
operators. Expressions almost always yield numeric results (integer or
floating-point values). For example, the expression
expr 8.2 + 6
evaluates to 14.2. For some examples of simple expressions, suppose the
variable a = 3 and b = 6. Then the commands shown below will produce the
value after the ->
set a 3
set b 6
expr 3.1 + $a -> 6.1
expr 2 + "$a.$b" -> 5.6
expr [ splitreply [omega] ] / 2.0 ->
omega axis position / 2.0
Note the use of square brackets [] for command substitution.
Math functions
Tcl supports the following mathematical functions in
expressions:
acos
cos
hypot
sinh
asin
cosh
log
sqrt
atan
exp
log10
tan
atan2
floor
pow
tanh
ceil
fmod
sin
Note you must use the expr command to invoke these
functions eg,
expr cos(0)
set pi [expr acos(-1)]
expr sin($pi)
Each of these functions invokes the math library function of the same name; see the
manual entries for the library functions for details on what they do. Tcl also
implements the following functions for conversion between integers and floating-point
numbers and the generation of random numbers:
abs(arg),
double(arg) ,
int(arg),
rand(arg),
round(arg),
srand(arg).
if - execute scripts conditionally
if expr1 then
body1
elseif expr2 then
body2
elseif...
else
bodyN
The if command evaluates expr1 as an
expression (in the same way that expr evaluates its argument). The
value of the expression must be a boolean (a numeric value, where 0 is false and
anything is true, or a string value such as "true" or "yes" for true and "false" or "no"
for false); if it is true then body1 is executed by passing
it to the Tcl interpreter. Otherwise expr2 is evaluated as an
expression and if it is true then body2 is executed, and so
on. If none of the expressions evaluates to true then bodyN
is executed. The then and else arguments are
optional "noise words" to make the command easier to read. There may be any number of
elseif clauses, including zero. BodyN
may also be omitted as long as else is omitted too. The return value
from the command is the result of the body script that was executed, or an empty string
if none of the expressions was non-zero and there was no
bodyN.
"if"
set a 3
if {$a == 3} {puts "a equals three"}
for - "for" loop
for start test
next
body
for is a looping command, similar in structure to the C
for statement. The start, next, and
body arguments must be Tcl command strings, and
test is an expression string. If a
continue command is invoked within body
then any remaining commands in the current execution of body
are skipped; processing continues by invoking the Tcl interpreter on
next, then evaluating test , and
so on. If a break command is invoked within
body or next , then the
for command will return immediately. The operation of
break and continue are similar to the
corresponding statements in C. for returns an empty string.
"for"
for {set x 0} {$x<10} {incr x} {puts "x is $x"}
while - execute script repeatedly as long as a condition is met
while test
body
The while command evaluates test as an
expression (in the same way that expr evaluates its argument). The
value of the expression must be a proper boolean value; if it is a true value then
body is executed by passing it to the Tcl interpreter.
Once body has been executed then
test is evaluated again, and the process repeats until
eventually test evaluates to a false boolean value.
continue commands may be executed inside
body to terminate the current iteration of the loop, and
break commands may be executed inside body to
cause immediate termination of the while command. The
while command always returns an empty string.
"while"
set x 0
while {$x<10} {
puts "x is $x"
incr x
}