??博主簡介
????云計算領(lǐng)域優(yōu)質(zhì)創(chuàng)作者
????華為云開發(fā)者社區(qū)專家博主
????阿里云開發(fā)者社區(qū)專家博主
??交流社區(qū):運(yùn)維交流社區(qū) 歡迎大家的加入!
grep -q 簡介
用于if邏輯判斷 安靜模式,不打印任何標(biāo)準(zhǔn)輸出。如果有匹配的內(nèi)容則立即返回狀態(tài)值0。文章來源:http://www.zghlxwxcb.cn/news/detail-708725.html
用法
grep -q 參數(shù)[索要查找的內(nèi)容] 文件名
實(shí)例
實(shí)例1
[root@localhost ~]# cat a.txt ## 測試數(shù)據(jù)
d e j
s q u
z c b
[root@localhost ~]# grep "s" a.txt ## 直接輸出匹配結(jié)果
s q u
[root@localhost ~]# echo $? ## 輸出0表示匹配成功
0
[root@localhost ~]# grep -q "s" a.txt ## -q選項表示靜默輸出
[root@localhost ~]# echo $? ## 輸出0表示匹配成功
0
實(shí)例2
[root@localhost ~]# cat a.txt ## 測試數(shù)據(jù)
nihao
nihaooo
hello
[root@localhost ~]# grep hello a.txt ## 直接輸出匹配結(jié)果
hello
[root@localhost ~]# echo $? ## 輸出0表示匹配成功
0
[root@localhost ~]# grep -q hello a.txt ## -q選項表示靜默輸出
[root@localhost ~]# echo $? ## 輸出0表示匹配成功
0
#判斷是否查找到hello文字,如果有則輸出yes,沒有則輸出no;使用靜默輸出
[root@localhost ~]# if grep -q hello a.txt ; then echo yes;else echo no; fi
yes
[root@localhost ~]# if grep -q word a.txt; then echo yes; else echo no; fi
no
文章來源地址http://www.zghlxwxcb.cn/news/detail-708725.html
到了這里,關(guān)于【Linux】grep -q用法詳解的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!