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

 

  1. grep -q "abcdef" a.txt
  2. if [ $? == 0 ]; then#查找成功
  3.     echo xxoo
  4. else
  5.     echo ooxx
  6. fi

相关文章:

  • 2021-04-04
  • 2021-09-16
  • 2022-12-23
  • 2021-06-07
  • 2021-10-12
  • 2021-08-22
  • 2022-01-03
  • 2022-12-23
猜你喜欢
  • 2021-11-21
  • 2022-12-23
  • 2022-03-01
  • 2021-12-27
  • 2022-01-04
  • 2021-11-20
相关资源
相似解决方案