2012-09-22 28 views
0

林的命令做这个工作正常读一个变量是在终端

[email protected]:~$ read command 
ps -fea 
[email protected]:~$ $command 
UID  PID PPID C STIME TTY   TIME CMD 
root   1  0 0 23:34 ?  00:00:02 /sbin/init 
root   2  0 0 23:34 ?  00:00:00 [kthreadd] 
root   3  2 0 23:34 ?  00:00:00 [ksoftirqd/0] 
... 

但试图运行此命令失败时...

[email protected]:~$ read command 
ps -fea | grep bash 
[email protected]:~$ $command 
ERROR: Garbage option. 
********* simple selection ********* ********* selection by list ********* 
-A all processes      -C by command name 
-N negate selection     -G by real group ID (supports names) 
-a all w/ tty except session leaders -U by real user ID (supports names) 
-d all except session leaders   -g by session OR by effective group name 
-e all processes      -p by process ID 
T all processes on this terminal  -s processes in the sessions given 


    ... 
    ... 
    ... 
    ... 
         ********* misc options ********* 
-V,V show version  L list format codes f ASCII art forest 
-m,m,-L,-T,H threads S children in sum -y change -l format 
-M,Z security data  c true command name -c scheduling class 
-w,w wide output  n numeric WCHAN,UID -H process hierarchy 

有谁知道怎么回事上??

Thanx in advanced!

+0

Try execute \'command \' – Michael

回答

4

当你的命令行由一个单一的参数来进行扩展,如

$ $command 

bash中执行以下操作:

  1. 展开参数,并且在执行字分割结果。
  2. 将第一个单词作为简单命令执行,将其余单词作为参数传递。

就是这样。没有进一步解析扩展来处理任何类型的复合命令或变量赋值或重定向的结果。

什么更复杂的需求传递到了eval命令,但只有,如果你完全了解eval作品,知道评估的任意字符串的安全隐患。在我看来,eval应该被认为是最后的手段,而不是方便。

+0

非常感谢!我会尽力了解更多关于eval的信息! – coolerking

5

您不能将重定向放入变量中。让shell执行内容。

$ bash -c "$command"