2014-01-21 51 views
1

我試圖存儲ps的輸出並稍後進行比較。
我使用下列行:
ps -ef | wc -l將製表符添加到該行的前面

siteminder_running=`ps -ef | grep $iplanet_home | grep LLAWP | wc -l` 

當我試圖比較我發現,變量具有在數目的前方的標籤的輸出。

這是輸出:

-  0- value 

可能是什麼問題呢?

+4

我不知道是什麼問題,但你可以通過消除'wc'來繞過它:你可以替換'grep LLAWP |帶'grep -c LLAWP'的wc -l'。 – ruakh

+2

這是輸出什麼?請更新您的問題以顯示產生該輸出的確切命令。 –

回答

0

提供大多數Unix和GNU/Linux版本的wc(1)實用程序將打印到由TAB分隔的用戶終端輸入文件名和計數(行/字符/您要求它打印的內容)。

在標準輸入的情況下,沒有輸入文件名,導致計數前面有一個TAB。

有幾種方法來規避這一點,例如:

ps -ef | grep $iplanet_home | grep LLAWP | wc -l | awk '{ printf "%d\n", $0 }' 
printf $(ps -ef | grep $iplanet_home | grep LLAWP | wc -l) 
ps -ef | grep $iplanet_home | grep LLAWP | wc -l | sed -e 's/^[ \t]*//' 

至於說,這只是一個例子,也有從字面上完成此的幾十個辦法。

+1

和上面評論中指出的ruakh一樣,最有效的方法是'... | grep -c LLAWP'。祝你們好運。 – shellter

1

像ruakh指出,你可以使用grep -c

siteminder_running=`ps -ef | grep $iplanet_home | grep -c LLAWP` 

對於一般的固定空白,我用xargs echo這樣的:

siteminder_running=`ps -ef | grep $iplanet_home | grep LLAWP | wc -l | xargs echo`