2011-05-24 25 views
3

我试图在linux中构建一个脚本 - bash,刚接触它。我想将命令ps -ef的输出作为变量进行分配。有人可以帮助我吗?微不足道的myVariable = ps -ef不幸的是不工作。获得ps -ef作为变量的结果

+0

好的,我有一些答案。 要么'myVariable ='ps -ef'' 要么'myVariable = $(ps -ef)' 非常感谢很多家伙的回答。 – S4M 2011-05-24 13:07:09

回答

1

您想要:

myVariable=`ps -ef` 

或:

myVariable=$(ps -ef) 

请注意'='符号周围没有空格。

4
myVariable=$(ps -ef) 
# or 
myVariable=`ps -ef` 
+0

+1:第一个通常会更好,因为您可以将它们嵌套。 – paxdiablo 2011-05-24 13:03:13

+0

在第一种情况下语法突出显示(至少在emacs中),并且不在第二种情况。 – 2011-08-27 16:04:14

2

尝试:

myVariable=`ps -ef` # back tic, upper left on most keyboards 

然后解析结果的真正乐趣就可以开始

+0

它不工作: '[root @ stpvsrv0018 RServer]#myVariable ='ps -ef' -bash:myVariable:command not found' – S4M 2011-05-24 13:03:01

+0

这是因为它错了。 – 2011-05-24 13:03:37

+0

我的不好,失去'=' – Andrew 2011-05-24 13:05:31

0

MYVARIABLE =`PS -ef`或MYVARIABLE = $(PS -ef)