Problem
I have two Tcl scripts running under Linux: script1.tcl launches script2.tcl. I like to pass all command-line parameters (that is, $argv) from script1.tcl to script2.tcl and then some. However, the $argv parameter arrives to script2.tcl were groupped into one token instead of separate tokens. An example will make it clearer:
Let say, I invoked script1.tcl like this:
tclsh script1.tcl –file config.txt –userCount 5
Then in script1.tcl, I see that $argv is broken into the following tokens:
–file
config.txt
–userCount
5
This is the behavior I want. Now, in script1.tcl, I spawn the second script, I pass along $argv along with some other parameters, like this:
exec tclsh script1.tcl $argv –userIndex 0
Then in script2.tcl, $argv’s token are broken down as shown:
–file config.txt –userCount 5
–userIndex
0
Notice that the first token is everything that I passed into script1.tcl and Tcl group them together as one token instead of separate ones. This is not the behavior I am looking for so I set out to solve it.
Solution
After a bit of searching around, I found a solution from the Tickler Wiki. Basically, instead of one exec line, I have to do this:
set cmd [list exec tclsh script2.tcl]; # Construct the command line
set cmd [concat $cmd $argv --userIndex 0]; # append the arguments
if {[catch $cmd result]} {
puts “Exec failed with error: $result”
} else {
puts “Exec passed, output:”
puts $result
}
This solution works because the concat command in the second line broke $argv into pieces and concatenate it to $cmd. While this solution works, it is a bit of a hack. Without detailed comments, I will not know what to do with it six months from now. If you have a different (or better) solution, please post your comments here.
