Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing arguments from command line into a tcl script

Tags:

bash

tcl

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?

like image 576
pypep278 Avatar asked Jun 24 '14 00:06

pypep278


People also ask

How can you pass command line arguments to a 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.

Can you pass command line arguments?

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.

How are environment variables used in Tcl script?

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.


1 Answers

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]]
    }
like image 172
thor Avatar answered Nov 15 '22 02:11

thor