grep -q用於if邏輯判斷
突然發現grep -q 用於if 邏輯判斷很好用。
-q 參數,本意是 Quiet; do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected. 中文意思為,安靜模式,不打印任何標准輸出。如果有匹配的內容則立即返回狀態值0。
小應用
# cat a.txt
nihao
nihaooo
hello
# if grep -q hello a.txt ; then echo yes;else echo no; fi
yes
# if grep -q word a.txt; then echo yes; else echo no; fi
no