我想在 Tcl/Tk 中创建一个简单的控制台
我有两个问题。首先用 [glob *] 更改每个 *,而且,当我的条目包含“ls -a”时,它不理解 ls 是命令,-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
最佳答案
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
关于bash - 如何在 TCL 中使用 exec 将变量拆分为两个参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3709198/