避免的grep返回错误,当输入不匹配 [英] Avoid grep returning error when input doesn't match

查看:172
本文介绍了避免的grep返回错误,当输入不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在bash脚本写一张code来检查,如果一个程序已经运行的。
我曾经为了下面的搜索栏是否正在运行

I want to write in a bash script a piece of code that checks if a program is already running. I have the following in order to search whether bar is running

 foo=`ps -ef | grep bar | grep -v grep`

 grep -v grep

部分是确保grep的栏以ps结果未考虑

part is to ensure that the "grep bar" is not taken into account in ps results

在酒吧没有运行,foo是正确空。但我的问题就在于塔脚本有

When bar isn't running, foo is correctly empty. But my problem lies in the fact tha the script has

 set -e

这是终止脚本,如果一些命令返回一个错误的标志。
事实证明,当酒吧没有运行,grep的-v grep的不匹配任何东西和grep返回一个错误。我尝试使用-q或-s但无济于事。

which is a flag to terminate the script if some command returns an error. It turns out that when bar isn't running, "grep -v grep" doesn't match with anything and grep returns an error. I tried using -q or -s but to no avail.

有没有什么解决方案吗? THX

Is there any solution to that? Thx

推荐答案

当然:

ps -ef | grep bar | { grep -v grep || true; }

甚至

ps -ef | grep bar | grep -v grep | cat

这篇关于避免的grep返回错误,当输入不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆