TCL/期望-exec-如何使用参数执行程序 [英] TCL/Expect - exec - how to execute program with parameters

查看:152
本文介绍了TCL/期望-exec-如何使用参数执行程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在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}

expand运算符允许您指定要扩展的变量,之前,命令行被拆分为单个参数:

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/期望-exec-如何使用参数执行程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆