首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何执行OR grep (使用不同的GREP_COLOR设置)

如何执行OR grep (使用不同的GREP_COLOR设置)
EN

Unix & Linux用户
提问于 2017-03-23 13:20:35
回答 1查看 259关注 0票数 1

因此,我想强调动态motd上的服务状态。我目前正在对php-fpm使用以下命令:

代码语言:javascript
复制
service php5-fpm status|grep Active|cut -d':' -f2-

我已经尝试过几种解决方案来达到这个目的。下面两件事做得很好,当所有其他情况都正常时,就能检测到1/ 1。

代码语言:javascript
复制
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似乎不起作用。

代码语言:javascript
复制
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运行时,我得到以下输出:

代码语言:javascript
复制
+ 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 '.* \(.*\)'

所以..。我现在不知道,我希望有人能看到我在哪里做错事。

EN

回答 1

Unix & Linux用户

回答已采纳

发布于 2017-03-23 13:36:55

代码语言:javascript
复制
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
票数 2
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/353335

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档