因此,我想强调动态motd上的服务状态。我目前正在对php-fpm使用以下命令:
service php5-fpm status|grep Active|cut -d':' -f2-我已经尝试过几种解决方案来达到这个目的。下面两件事做得很好,当所有其他情况都正常时,就能检测到1/ 1。
service php5-fpm status|grep Active|cut -d':' -f2-|GREP_COLOR='1;32' grep --color=always " active \(.*\)"
service php5-fpm status|grep Active|cut -d':' -f2-|GREP_COLOR='1;31' grep --color=always -E ".* \(.*\)"我试图做的是使用||将它们放在同一个命令中。当第一个grep返回0时,这很好,但是当第一个grep失败并返回1时,第二个grep似乎不起作用。
service php5-fpm status|grep Active|cut -d':' -f2-|(GREP_COLOR='1;32' grep --color=always -E " active \(.*\)" || GREP_COLOR='1;31' grep --color=always -E ".* \(.*\)")当使用bash -x运行时,我得到以下输出:
+ GREP_COLOR='1;32'
+ grep --color=always -E ' active \(.*\)'
+ cut -d: -f2-
+ grep Active
+ service php5-fpm status
+ GREP_COLOR='1;31'
+ grep --color=always -E '.* \(.*\)'所以..。我现在不知道,我希望有人能看到我在哪里做错事。
发布于 2017-03-23 13:36:55
Where will the 2nd grep get it's input from when the 1st grep fails?
Coz, grep1 consumes all the stdin with nothing left for grep2.In the
case of grep1 succeeding, grep2 never runs so is not an issue.
We may rig it up like the following to achieve what you want:
#/bin/sh
service php5-fpm status |
grep Active |
cut -d':' -f2- | tee /tmp/log |
GREP_COLOR='1;32' grep --color=always -E " active \(.*\)" - ||
GREP_COLOR='1;31' grep --color=always -E ".* \(.*\)") /tmp/loghttps://unix.stackexchange.com/questions/353335
复制相似问题