grep -q判断是否包含字符串

时间:2015-03-11 10:40:15   收藏:0   阅读:1203

-q 参数,本意是 Quiet; do not write anything to standard output.  Exit immediately with zero status if any match is found, even if an error was detected.   中文意思为,安静模式,不打印任何标准输出。如果有匹配的内容则立即返回状态值0。

经常碰到要判断某个字符串变量是否包含另外一个字符串的问题,使用grep判断,我们其实只想要返回值,不想有任何输出,这时用-q可解决该问题。

a="aa bb cc dd"
echo $a | grep "aa"
if [ $? -eq 0 ]; then
echo "This string is include aa."
else
  echo "This string is not include aa."
fi

 

原文:http://www.cnblogs.com/ManMonth/p/4329135.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!