用shell查看文件中是否包含某字符

查看abc.txt文件中是否包含a字符

方法一:

grep a abc.txt >/dev/null && echo 'have'

$?变量:若执行成功,返回0,失败,返回1。

方法二:

if [ `grep -c a abc.txt` -eq 0 ];then
	echo 'not have'
else
	echo 'have'
fi

-c参数:表示输出匹配的行数

猜你喜欢

转载自suo.iteye.com/blog/1161377