0

I am writing scripts in tcl for a project I am working on.

I wanted to automate things as much as possible and wanted to not touch the source code of the script as far as possible. I want to run the main script file from a .bat or .job file sort of thing where I pass the command to execute the script along with the arguments.

I have referred to this post on stackoverflow:

How to run tcl script in other tcl script?

And have done pretty much the same thing. However, since my script is naked code rather than a single huge proc, I dont have the "args" parameter to read the arguments I wanted to pass.

For example, if script1.tcl is the main file containing the naked code, I want a file script2.job or script2.bat such that,

<command-to-run-script1.tcl> <mandatory-args> <optional-args> 

is the content of the file.

Any suggestions on how I can implement the same?

Community
  • 1
  • 1
pypep278
  • 187
  • 2
  • 4
  • 18
  • You have access to the `argv` array and the `argc` value among Tcl's [global variables](http://tcl.tk/man/tcl8.5/TclCmd/tclvars.htm#M38) – glenn jackman Jun 09 '14 at 19:32
  • Hi Glenn, you mean that I can use argv and argc even though I dont have a single proc in my script1.tcl file? Could you throw some more light on how this can done? – pypep278 Jun 09 '14 at 20:07

1 Answers1

2

To run a Tcl script, passing in some arguments, do:

tclsh script1.tcl theFirstArgument theSecondArgument ...

That's how it works in CMD scripts/BAT files on Windows, and in shell scripts on all Unixes. You might want to put quotes around some of the arguments too, but that's just absolute normal running of a program with arguments. (The tclsh might need to be tclsh8.5 or tclsh85 or … well, it depends on how it's installed. And script1.tcl might need to be a full path to the script.)

Inside the script, the arguments (starting at theFirstArgument) will appear in the Tcl list in the global argv variable. Note that this is not args, which is a feature of procedures. There are lots of ways of parsing the list of arguments, but any quoting supplied during the call itself should have been already stripped.

Here's a very simple version:

foreach argument $argv {
    puts "Oh, I seem to have a >>$argument<<"
}

You probably need something more elaborate! There's many possibilities though, so be sure to be exact to get more focussed ideas.


If you're calling Tcl from another Tcl script, you need to use exec to do it. On the other hand, you can make things a bit easier for yourself in other ways:

exec [info nameofexecutable] script1.tcl theFirstArgument theSecondArgument ...

The info nameofexecutable command returns the name of the Tcl interpreter program (often tclsh8.5 or wish86 or …)

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • Hi Donal, Thanks for that response! However, I still have a confusion regarding how to store the arguments that I pass in the "exec" command into a variable in the main script file and then use it to toggle options in my script? Do note that my main script file does not have any proc and thats the way it is supposed to be for my requirements. Appreciate any help! – pypep278 Jun 09 '14 at 20:17
  • @user2784743 You never need to use a procedure. It might be a good idea to do so, but you don't _need_ to. Parsing arguments is awkward because there's many possible formats you might want to parse (it isn't “one per programmer” but it's nearly as bad!) and I don't want to tell you of every possible one; you need to edit your question to be more exact for me to go into depth. – Donal Fellows Jun 09 '14 at 20:47
  • Yes Donal, I am aware of how to parse the argument list. However, my question was pertaining to these two important facts: 1. I am not using a procedure. 2. I am using command line arguments while executing the script. Now, if I parse this command, I was wondering if I can access the arguments in the main script using argv directly or do I have to do something special for that? – pypep278 Jun 09 '14 at 20:50
  • @user2784743, yes, you can use argv directly. – glenn jackman Jun 09 '14 at 22:40