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?
Arguments can be passed to the script when it is executed, by writing them as a space-delimited list following the script file name. Inside the script, the $1 variable references the first argument in the command line, $2 the second argument and so forth.
It is possible to pass some values from the command line to your C programs when they are executed. These values are called command line arguments and many times they are important for your program especially when you want to control your program from outside instead of hard coding those values inside the code.
Environment variables are available to Tcl scripts in a global associative array env . The index into env is the name of the environment variable. The command puts "$env(PATH)" would print the contents of the PATH environment variable.
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]] }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With