6
votes

I have a TCL script that has many parameters defined as arguments. I intend to create a job file where I can execute the .tcl script with different combinations of the parameters without manual intervention.

What I mean is the following:

Job File (run.sh):

./main.tcl arg1 arg2 arg3 arg4 
./main.tcl arg1 arg3 arg5

Now I want to be able to pass the command line argument array "argv" for each run mentioned in run.sh as an array into the main.tcl script so that the options are set accordingly within the script prior to execution.

Is there a way to link the .sh script and the .tcl script?

2
Inside your shell script you can refer to the list of all arguments as $@. So just say main.tcl $@ - John C
@JohnC - thanks for the reply! ok, so if i do ./main.tcl $@ inside my run.sh, then how can i retrieve the arguments inside of main.tcl? using argv? - pypep278
I think the answer below covers that for you. - John C

2 Answers

6
votes

Per online document here:

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]]
    }
0
votes

In addition to the above answer, I found the following helpful:

For running within QuestaSim/ModelSim/Quartus I followed the following resource: http://www.alteraforum.com/forum/showthread.php?t=3034

Typically we can run .tcl scripts with the source command, but this doesn't inherently process arguments like base .tcl scripts can use normally. So by using a process, you can achieve the same thing.