Shell script to create loop for command

130 views Asked by At

So i have a tcl code that i need to run in ns2. As we all know i just have to type 'ns abc.tcl' in terminal. In my abc.tcl code i have a variable x which i need to change and run the code. Is there any way i can write a script that will change the value of x and run 'ns abc.tcl' in terminal, then change the value again and run 'ns abc.tcl' in terminal again for a set of values for x. I believe i need to write a shell script but i don't know anything about that. Can you tell me the format i should write the script in like what should i write first and where do i write my values of x and how to make it run 'ns abc.tcl in terminal: 'function()' 'do' 'done' etc... If you can direct me to specific links about that would be helpful.

1

There are 1 answers

0
Donal Fellows On

The easiest way, providing it works, is to pass the value in as an argument.

  1. Invoke your code as ns abc.tcl TheValueToPassIn.

  2. Access the value within your code by indexing into the argv global variable with lindex, which should contain a list of all arguments after the script name:

    set myValue [lindex $::argv 0]
    

However, it's possible that that won't work (depending on exactly what the ns program does). If so, pass the value in inside an environment variable:

  1. Invoke your code as MYVAR=TheValueToPassIn ns abc.tcl.

  2. Access the value within your code by looking in the global env array:

    set myValue $::env(MYVAR)
    

There are many other ways to do it, but those two are very easy.