Question

I am a beginner of Linux (Ubuntu 12.10). I want to create a batch file to control my TCL file, and I want my TCL to run for several times.

For example, I want the batch file to do the following things:

for(i=1;i<100;i++){
    sudo ns test.tcl $i
}

I want my tcl file to accept parameters ($i) so that I can use the parameter in the tcl file.

Can anyone tell me what can I do? or give me a direction?

Thanks in advance.

ps. I wrote the loop inside my TCL file but failed. My code was like:

for(i=1;i<100;i++){

    set ns [new Simulator]
    .... 
    ....(my NS code)
    ....
    $ns run

}

It ran only for $i==1, so I am thinking of writing the loop outside the tcl script.

PS2. Hi guys, I am sorry for not expressing myself clear. I did follow the TCL syntax. My real code is the following:

for {set i 1} {$i < 100} {incr i} {
    set ns [new Simulator]
    ...
    ...
    ...
    $ns run
}

And I solved this problem by rearranging my code to the following:

set ns [new Simulator]
for {set i 1} {$i < 100} {incr i} {
    ...
    ...
    ...
}
$ns run

Now it runs 99 times. I don't know why, though :P

Thank you guys anyway :)

Était-ce utile?

La solution

Um, are you sure you're writing Tcl? This isn't the syntax for Tcl. What you might be looking for is:

for {set i 1} {$i < 100} {incr i} {
    set ns [new Simulator]
    .... 
    ....(my NS code)
    ....
    $ns run
}

You use braces everywhere. The only time I can think of using brackets is for arrays, but I don't think I know Tcl deep enough to ascertain this, and your start conditions, test and next commands are in their own braces and incr i is equivalent to i++.

Now, new is not a built-in function in Tcl. Do you have a proc named new somewhere? If you don't, then you'll get errors.

Autres conseils

A Tcl script is very simple, it consists of a number of commands and each command consists of a command name followed by a number of arguments. Even control structures are commands. The for command takes 4 arguments: a start script, a test expression, a next script and a body script. Like all other command arguments (and command names), you can quote for's arguments any way you want, or even use variables and other substitutions for some of them, but usually you don't want that before they're sent to the command, so you use braces:

for {set i 1} {$i < 100} {incr i} {
  # ...
}

The first thing I notice is the expression

for(i=1;i<100;i++){

This loop only runs 99 times, which might not be what you want, besides the wrong syntax. Others suggest the correct for loop, so please go with that. If you still think Tcl for loop is too complicated, may I suggest an alternative:

package require Tclx

loop i 0 100 {
    # i will run from 0 to 99
}

The only disadvantage of this approach is you have to pull in the Tclx package, but if your script already uses Tclx, why not?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top