heyhey
heyhey

Reputation: 301

How to intepret " " in a string list in TCL?

I want to construct a complicate variable, let's see :

set entry_arg [lindex $argv 1]
set command_to_launch {extrernal_command_tool -option "set var $entry_arg" -other_option}
exec $command_to_lauch

The command is launched, no problem ...

but, the problem is that the tool get $entry_arg and crash ... This is due to the " " that don't allow tcl to interpret de variable

set command_to_launch {extrernal_command_tool -option "set var [lindex $argv 1]"  -other_option}

Have the same problem !

How can I solve it ?

Upvotes: 0

Views: 205

Answers (2)

mr.spuratic
mr.spuratic

Reputation: 10107

The problem isn't "", it's {}. Try:

set entry_arg [lindex $argv 0]
set command_to_launch "extrernal_command_tool -option $entry_arg -other_option"
set output [eval exec $command_to_lauch]

You need to learn about interpolation, quoting and braces, like this: Understanding the usage of braces

(Also, I suspect you want to use [lindex $argv 0] -- TCL uses the variable argv0 as the program name, and the arguments start at index 0, unlike C and other languages.)

Edit: You might actually mean this, if you really want a quoted argument like "set var xxx":

    set command_to_launch "extrernal_command_tool -option \"set var $entry_arg\" -other_option"

Upvotes: 1

Donal Fellows
Donal Fellows

Reputation: 137567

You should construct the contents of command_to_launch with the list command.

set command_to_launch [list \
    extrernal_command_tool -option "set var [lindex $argv 1]" -other_option]

You would the run this with either one of these:

exec {*}$command_to_launch           ; # For 8.5 or 8.6
eval exec $command_to_launch         ; # For 8.4 or before

Pick the right one for which version of the language you're using, of course.

Upvotes: 6

Related Questions