版权声明:Summer https://blog.csdn.net/csdnyanglei/article/details/82109450
#!/bin/bash
# file name : silent_grep.sh
# usage : 测试一个文本里是否含有某个字符串
if [ $# -ne 2 ] ;then
echo "Usage:$0 match_text file name"
echo 1
fi
match_text=$1
filename=$2
# -q 静默搜索,不打印搜索结果,只返回是否匹配上(0)或未匹配上(非0)
grep -q "$match_text" $filename
if [ $? -eq 0 ];then
echo "The text exists in the file"
else
echo "Text does not exist in the file"
fi