Meruj Martirosyan
Meruj Martirosyan

Reputation: 67

How to call one tcl script by other script and pass argument?

I would like to call one TCL script inside the other? I tried the following command:

exec tclsh print.tcl

Inside of print.tcl is written:

puts "Second script"

The program runs without any errors but does not show any output.

What is wrong?

Upvotes: 0

Views: 927

Answers (2)

glenn jackman
glenn jackman

Reputation: 246817

Alternately, the 2nd script is a Tcl script, so you could do

source print.tcl

which will do what you want, I think.


Since the print script needs argv, you need to set that in the current script:

set argv [list foo bar baz]
source print.tcl

and if you're invoking source from within a procedure, you'll probably want to execute the print code at the top-level scope, not the procedure scope

proc myProc {} {
    set ::argv [list foo bar baz]
    uplevel #0 {source print.tcl}
}
myProc

Upvotes: 0

Donal Fellows
Donal Fellows

Reputation: 137577

That value would be obtained via:

set output [exec tclsh print.tcl]
puts "The output was: $output"

If you want it to go directly to the user, you need to do a bit of redirection:

exec tclsh print.tcl >@stdout

or possibly even this (if the script prints errors that you don't want to capture):

exec tclsh print.tcl >@stdout 2>@stderr

On POSIX systems (i.e., not Windows), you can even send the output to the user and capture it in the calling script at the same time:

set output [exec tclsh print.tcl | tee /dev/tty]
puts "The output was: $output"

Upvotes: 1

Related Questions