OR grepを実行する方法(他のGREP_COLOR設定を使用)

OR grepを実行する方法(他のGREP_COLOR設定を使用)

したがって、動的motdのサービス状態を強調したいと思います。私は現在php-fpmで次のコマンドを使用しています。

service php5-fpm status|grep Active|cut -d':' -f2-

これを達成するためにいくつかの解決策を試しました。次の2つは、1/すべてが正常である場合2/他のすべての状況を検出する優れたタスクを実行します。

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を返すと正常に動作しますが、失敗し、1を返すと2番目の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 '.* \(.*\)'

だから…今はよく分からないし、誰かが私が何を間違っているのか知ってほしい。

ベストアンサー1

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/log

おすすめ記事