Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Command line parameters?

Status
Not open for further replies.

zarchy

Technical User
Mar 1, 2006
4
US
Hi,

I've been searching the web like crazy to find an answer to what I believe is a simple question. I am running tcl scripts from unix and would like to be able to pass a command line parameter to a tcl script. Can this be done?

Thanks in advance!
 
Thanks, but I didn't quite see what I was looking for in the link you posted. What is the syntax for passing a parameter to a tcl script? Also, what is the syntax for using the passed-in paramter inside the script?

Thanks again!
 
As it says in the referenced thread, there is a reserved list variable, argv. To use command line arguments, your script must pull the arguments out of that variable. Another reserved variable, argc, tells the script how many elements the list, argv, contains. I use that sometimes to set a parameter in my script, then, if argc>0, I use the passed argument to reset it, for example.

To pass the arguments in you simply type them after the script name. Let's say your script is "myScript":
tclsh myScrippt arg1 arg2 ...

Then inside your script arg1 is the value of lindex $argv 0

_________________
Bob Rashkin
 
Hi

Maybe an example too :
Code:
[blue]master #[/blue] cat zarchy.tcl
#!/usr/bin/tclsh

puts "The $argv0 script was calles with $argc arguments :"

for {set i 0} {$i<[llength $argv]} {incr i} {
  puts " - $i : [lindex $argv $i]"
}

[blue]master #[/blue] zarchy.tcl wants some arguments
The ./zarchy.tcl script was calles with 3 arguments :
 - 0 : wants
 - 1 : some
 - 2 : arguments

Feherke.
 
Followup -


I am still having trouble. I must confess that my knowledge of tcl is very elementary. The things I am trying to do are fairly simple but I am having trouble just the same. I'll try to describe it:

I have a tcl script that needs to call a second tcl script and pass some parameters to it. I was previously using the 'source' command to call the second script and that was working fine, except when I tried to add parameters to the source command. That gave me errors. Apparently the 'source' command does not support parameters.

Based on the examples listed in this thread, I replaced the 'source' command with tclsh. This allowed me to pass the parameters and I was able to successfully extract these parameters in the second script. However, when it got to the parts of this script where it had to actually execute commands for the tool I am running, it did not understand the commands. (This did not happen when I called the second script using 'source'.)

So as you can see, I am getting very frustrated.
 
First of all, using source is the more elegant approach; you only use the single instance of Tcl. What the source command does is load the referenced script and begin stepping through it. So, in that case, rather than pass parameters (arguments) you just need to set the values of the variables you will use in the sourced script.

Alternatively, the sourced script can be a proc (thing subroutine) that has arguments. Then, sourcing the script will cause no execution until the proc is called, the arguments passed in the calling statement.

As far as why the second instance of Tclsh does not permit the execution of your external program, I'd have to see more of what you're trying to do.

_________________
Bob Rashkin
 
Hi

Yes, I missed too the possibility to pass parameters with the [tt]source[/tt] command. :-(

I used the poor man's parameter passing for this : I put all the values in a list, then the sourced code parsed that list instead of [tt]argv[/tt].

But if you post some code and error messages, probably someone will be able to suggest something better.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top