TCL/Expect - exec - 如何使用参数执行程序 [英] TCL/Expect - exec - how to execute program with parameters
问题描述
我正在 tclsh
中试验 TCL 命令 exec
,这是我的结果:
I am experimenting with TCL command exec
in tclsh
and here are my results:
% set show_me_dir "ls"
ls
% exec $show_me_dir
VboxSharedFolder
% set show_me_dir "ls -la"
ls -la
% exec $show_me_dir
couldn't execute "ls -la": no such file or directory
% set show_me_dir {ls -la}
ls -la
% exec $show_me_dir
couldn't execute "ls -la": no such file or directory
% ls -la
total 141
d---------+ 1 wakatana Domain Users 0 Jan 22 19:12 .
d---------+ 1 wakatana Domain Users 0 Apr 16 2014 ..
----------+ 1 wakatana Domain Users 20214 Jan 23 18:43 .bash_history
----------+ 1 wakatana Domain Users 1494 Apr 15 2014 .bash_profile
----------+ 1 wakatana Domain Users 7593 Jan 22 19:03 .bashrc
d---------+ 1 wakatana Domain Users 0 Jan 15 14:56 VboxSharedFolder
%
有人可以解释一下如何使用参数执行命令吗?
Can somebody please explain how can I execute command with arguments?
以下示例来自 扩展 Tcl 和 eval 文章中的参数列表使这里发生的事情大开眼界:
The following example from Expanding a list of parameters in Tcl and eval article was big eye opener of what is going on here:
变量 $action
仅扩展为字符串 "piemiddle apple"
AFTER 命令行已拆分为其各个参数:
The variable $action
is only expanded into the string "piemiddle apple"
AFTER the command line has been split into its individual parameters:
% set action {piemiddle apple}
% set $action
can't read "piemiddle apple": no such variable
结果:set
命令看到"一个参数,相当于:
Result: set
command "sees" one argument, equivalent to:
% set {piemiddle apple}
扩展运算符允许您指定要扩展的变量BEFORE命令行被拆分为单独的参数:
The expand operator allows you to specify that a variable is to be expanded BEFORE the command line is split into individual parameters:
% set action {piemiddle apple}
% set {*}$action
apple
结果:set
命令看到"了两个参数,相当于:
Result: set
command "sees" two arguments, equivalent to:
% set piemiddle apple
在早期版本的 Tcl 中,eval 命令是推荐的替代方法,并且今天仍然可用.
In earlier versions of Tcl, the eval command was the recommended alternative and it remains available today.
% set action {piemiddle apple}
% eval set $action
apple
另一个证明扩展运算符功能的例子:
Another examples which proves functionality of expansion operator:
% set {*}"name Linus"
Linus
% puts $name
Linus
%
%
% set distro Unbuntu
Unbuntu
% set {*}"linux $distro"
Unbuntu
% puts $linux
Unbuntu
%
%
最后发现 exec
需要命令作为它的第一个参数和第一个命令选项作为它的第二个参数等.
Finally the discovery that exec
needs command as it's first argument and first command option as it's second argument etc.
% exec "ls" "-la"
total 137
d---------+ 1 wakatana Domain Users 0 Jan 22 19:12 .
d---------+ 1 wakatana Domain Users 0 Apr 16 2014 ..
----------+ 1 wakatana Domain Users 20214 Jan 23 18:43 .bash_history
----------+ 1 wakatana Domain Users 1494 Apr 15 2014 .bash_profile
----------+ 1 wakatana Domain Users 7593 Jan 22 19:03 .bashrc
d---------+ 1 wakatana Domain Users 0 Jan 15 14:56 VboxSharedFolder
%
%
% exec "ls -la"
couldn't execute "ls -la": no such file or directory
推荐答案
为 exec
构建命令的最安全方法是使用 Tcl 的 list代码>.例如:
The safest way to build a command for exec
is to use Tcl's list
. For example:
% set tcl_version
8.5
% set cmd [list ls -l tmp]
ls -l tmp
% eval exec $cmd
total 32
-rw-r--r-- 1 pynexj staff 1176 Jan 23 23:24 file.txt
-rw-r--r-- 1 pynexj staff 1176 Jan 23 23:24 foo-1.dat
-rw-r--r-- 1 pynexj staff 1176 Jan 23 23:24 foo-2.dat
-rw-r--r-- 1 pynexj staff 1176 Jan 23 23:24 foo-3.dat
% exec {*}$cmd
total 32
-rw-r--r-- 1 pynexj staff 1176 Jan 23 23:24 file.txt
-rw-r--r-- 1 pynexj staff 1176 Jan 23 23:24 foo-1.dat
-rw-r--r-- 1 pynexj staff 1176 Jan 23 23:24 foo-2.dat
-rw-r--r-- 1 pynexj staff 1176 Jan 23 23:24 foo-3.dat
%
请注意,{*} 是 Tcl 的新语法8.5 有助于减少eval
的使用.
Note that {*} is a new syntax of Tcl 8.5 which can help reduce the uses of eval
.
这篇关于TCL/Expect - exec - 如何使用参数执行程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!