日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Linux?grep?-q用法示例詳解_linux shell

作者:A-劉晨陽 ? 更新時間: 2023-04-29 編程語言

Linux grep命令用于查找文件里符合條件的字符串。

grep指令用于查找內容包含指定的范本樣式的文件,如果發現某文件的內容符合所指定的范本樣式,預設grep指令會把含有范本樣式的那一列顯示出來。若不指定任何文件名稱,或是所給予的文件名為"-",則grep指令會從標準輸入設備讀取數據。

grep -q 簡介

用于if邏輯判斷 安靜模式,不打印任何標準輸出。如果有匹配的內容則立即返回狀態值0。

用法

grep -q 參數[索要查找的內容] 文件名

實例

實例1

[root@localhost ~]# cat a.txt            ## 測試數據
d e j
s q u
z c b

[root@localhost ~]# grep "s" a.txt       ## 直接輸出匹配結果
s q u

[root@localhost ~]# echo $?              ## 輸出0表示匹配成功
0

[root@localhost ~]# grep -q "s" a.txt    ## -q選項表示靜默輸出

[root@localhost ~]# echo $?              ## 輸出0表示匹配成功
0

實例2

[root@localhost ~]# cat a.txt            ## 測試數據
nihao 
nihaooo
hello

[root@localhost ~]# grep hello a.txt       ## 直接輸出匹配結果
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

原文鏈接:https://blog.csdn.net/liu_chen_yang/article/details/128967043

欄目分類
最近更新