2

我正在尝试存储 ps 的输出,然后进行比较。
我正在使用以下行:

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

当我尝试比较输出时,我发现变量在数字前面有一个制表符。

这是输出:

-       0- value

可能是什么问题呢?

4

2 回答 2

3

就像ruak指出的那样,您可以使用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`
于 2014-01-29T22:46:10.503 回答
1

大多数 Unix 和 GNU/Linux 发行版提供的 wc(1) 实用程序打印到用户终端输入文件名和计数(行数/字符数/您要求它打印的内容),由 TAB 分隔。

在标准输入的情况下,没有输入文件名,导致 count 前面只有一个 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]*//'

如前所述,这些只是示例,实际上有几十种方法可以实现这一点。

于 2014-01-21T15:12:32.900 回答