2010-09-14 60 views
3

我想在Tcl/Tk中創建一個簡單的控制檯如何用TCL中的exec在兩個參數中分割一個變量?

我有兩個問題。首先用[glob *]更改每個*,但是當我的條目包含「ls -a」時,它不理解ls是命令並且第一個arg是-a

我該如何設法做到這一點?

感謝

proc execute {} { 
    # ajoute le contenu de .add_frame.add_entry 
    set value [.add_frame.add_entry get] 
    if {[string compare "$value" ""] == 1} { 
    .text insert end "\n\n% $value\n" 
     .text insert end [exec $value] 
    .add_frame.add_entry delete 0 end 
    } 
} 

frame .add_frame 

label .add_frame.add_label -text "Nouvel élément : " 
entry .add_frame.add_entry 
button .add_frame.add_button -text "Executer" -command execute 
button .add_frame.exit_button -text "Quitter" -command exit 

bind .add_frame.add_entry <Return> execute 
bind .add_frame.add_entry <KP_Enter> execute 
bind . <Escape> exit 
bind . <Control-q> exit 

pack .add_frame.add_label -side left 
pack .add_frame.exit_button -side right 
pack .add_frame.add_button -side right 
pack .add_frame.add_entry -fill x -expand true 

pack .add_frame -side top -fill x 

text .text 
.text insert end "% Tcl/Tk Console" 

pack .text -side bottom -fill both -expand true 

回答

7

使用Tcl 8.5答案很簡單,使用這樣的:

exec {*}$value 

在8.4之前,這句法並不存在。這意味着,很多人都寫了這個:

eval exec $value 

但在現實中,安全的版本就是其中之一:

eval exec [lrange $value 0 end] 
eval [linsert $value 0 exec] 

當然,如果$value是直接來自用戶,然後你最好使用系統shell,因爲更多的用戶期待那種語法來評價它:

exec /usr/bin/bash -c $value 
+1

+1的建議「SH -C $值」 - 你可能不希望建立在Tcl的一個shell解釋。 – 2010-09-14 15:09:59

+0

但是,如果你真的想在Tcl中模擬shell語法,你可能想看看我的代碼http://wiki.tcl.tk/gush,特別是tokeinise和shellrun過程。 – 2010-09-15 18:58:28

相關問題