charly
charly

Reputation: 956

tcl exec with file in parameter doesn't work

I'm working on a file explorer inTCL/Tk and I want to add a little thing to execute commands with the current selection (using %l %f) %l executing with the full list and %f executing the commmand with each file. my only problem is that if I type a command like "gedit" for eg it works but as soon as I type a command with argument it doesn't work ... I've been looking everywhere and I don't get it... If someone could help me... btw getl var Name is a function that returns me a FileName in full path (/home/...) and if I return the string that is supposed to be executed and put it in a terminal it works...

Here is the code:

proc tl_exec {liste command } {
#lorsqu'il faut effectué la commande avec la liste en param
if { [string first "%l" $command] > 0} {
  foreach v $liste {
     lappend args [getl $v Name]
  } 
    set com [string map [list "%l" [join $args " "] ] $command ]
  puts $com
    set val [exec [split $com " "] ]
} elseif { [string first "%f" $command] > 0} {
#lorsqu'il faut effectué la commande pour chaque fichier        
  foreach v $liste {
            set com [string map list ["%f" [getl $v "Name"] ] $command ]
            lappend val [ exec [split $com " "] ]
    }
} else {
#lorsqu'on a pas de fichiers 
    set val [exec $command]
}
}

Thanks a lot

Upvotes: 1

Views: 3814

Answers (1)

schlenk
schlenk

Reputation: 7237

Your code has more than a single problem, it will probably break with special chars or spaces in filenames too, as you do not quoting at all.

But you are right about exec considering everything as a single command.

set val [exec [split $com " "] ]

does not do what you expect, split returns a list, but does not automagically turn that list into extra args for exec.

If you use Tcl 8.5 you can try:

set val [exec {*}[split $com " "] ]

which turns the list into single arguments to pass to exec.

But the code you use is brittle in general, as you do not handle any exit codes or programs writing to stderr, so a more elaborate solution would be needed to be robust.

Have a look at http://wiki.tcl.tk/1039 especially the discussions on the bottom of the page.

Upvotes: 5

Related Questions