开发者

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 ex开发者_JS百科ecuted 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜