---
title: "用shell查看文件中是否包含某字符"
author: "wode66"
date: "2011-08-30T14:25:42Z"
tags: []
categories: ["操作系统"]
slug: "shell"
draft: false
lastmod: "2011-08-30T14:26:37Z"
id: 1161377
url: "https://suo.iteye.com/blog/1161377"
---

查看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参数：表示输出匹配的行数