bash – 基于grep结果的echo

前端之家收集整理的这篇文章主要介绍了bash – 基于grep结果的echo前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要一个显示’是’或’否’的衬垫,无论grep是否找到任何结果.

我玩过grep -c,但没有成功.

怎么样:
uptime | grep user && echo 'yes' || echo 'no'
uptime | grep foo && echo 'yes' || echo 'no'

然后你可以安静下来:

uptime | grep --quiet user && echo 'yes' || echo 'no'
uptime | grep --quiet foo && echo 'yes' || echo 'no'

从grep手册页:

EXIT STATUS

Normally,the exit status is 0 if selected lines are found and 1 otherwise. But the exit status is 2 if an error occurred,unless the -q or –quiet or –silent option is used and a selected line is found.

猜你在找的Bash相关文章