linux 中 grep -q选项

 

1、linux 中 grep -q选项表示静默输出, 即不显式匹配结果

复制代码
root@DESKTOP-1N42TVH:/home/test2# ls
a.txt
root@DESKTOP-1N42TVH:/home/test2# cat a.txt            ## 测试数据
d e j
s q u
z c b
root@DESKTOP-1N42TVH:/home/test2# grep "s" a.txt       ## 直接输出匹配结果
s q u
root@DESKTOP-1N42TVH:/home/test2# echo $?              ## 输出0表示匹配成功
0
root@DESKTOP-1N42TVH:/home/test2# grep -q "s" a.txt    ## -q选项表示静默输出
root@DESKTOP-1N42TVH:/home/test2# echo $?
0
复制代码

 

2、grep命令用在 if判断语句中

复制代码
root@DESKTOP-1N42TVH:/home/test2# ls
a.txt  test.sh
root@DESKTOP-1N42TVH:/home/test2# cat a.txt
d e j
s q u
z c b
root@DESKTOP-1N42TVH:/home/test2# cat test.sh      ## 测试脚本, 利用grep匹配结果作为判断语句
#!/bin/bash
if grep -q "s" a.txt
then
        echo "s exit!"
else
        echo "s not exit!"
fi
root@DESKTOP-1N42TVH:/home/test2# bash test.sh         ## 判断成功,执行if语句
s exit!
root@DESKTOP-1N42TVH:/home/test2# sed 's/s/x/g' a.txt -i 
root@DESKTOP-1N42TVH:/home/test2# cat a.txt           ## 修改了测试数据
d e j
x q u
z c b
root@DESKTOP-1N42TVH:/home/test2# bash test.sh        ## 再次进行判断
s not exit!
复制代码

 

posted @   小鲨鱼2018  阅读(2840)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2021-04-24 R语言中while循环
2021-04-24 R语言中for循环和while循环求1-100的和
2021-04-24 R语言中if判断语句
2021-04-24 R语言中利用sapply函数提取列表中元素
2021-04-24 R语言中scale函数
2021-04-24 R语言中apply函数
2021-04-24 R语言中大小写转换
点击右上角即可分享
微信分享提示