2011-05-24 24 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)