签到成功

知道了

CNDBA社区CNDBA社区

shell脚本grep -q用于if条件判断

2022-03-17 13:17 1426 0 原创 Linux
作者: hbhe0316

-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。http://www.cndba.cn/hbhe0316/article/107881http://www.cndba.cn/hbhe0316/article/107881

http://www.cndba.cn/hbhe0316/article/107881
http://www.cndba.cn/hbhe0316/article/107881
http://www.cndba.cn/hbhe0316/article/107881
http://www.cndba.cn/hbhe0316/article/107881http://www.cndba.cn/hbhe0316/article/107881
http://www.cndba.cn/hbhe0316/article/107881
http://www.cndba.cn/hbhe0316/article/107881
http://www.cndba.cn/hbhe0316/article/107881

[root@node02:/home/test]$ cat 111.txt
hello
[root@node02:/home/test]$ cat check.sh
#!/bin/sh
grep -q "hello" 111.txt
if [[ $? == 0 ]];then
        echo "hello in 111.txt"
else
        echo "hello does not in 111.txt"
fi

grep -q "hello1" 111.txt
if [[ $? == 0 ]];then
        echo "hello1 in 111.txt"
else
        echo "hello1 does not in 111.txt"
fi

[root@node02:/home/test]$ sh check.sh
hello in 111.txt
hello1 does not in 111.txt

版权声明:本文为博主原创文章,未经博主允许不得转载。

shell

用户评论
* 以下用户言论只代表其个人观点,不代表CNDBA社区的观点或立场
hbhe0316

hbhe0316

关注

1.只有承认无知,才能装下新的东西; 2.进步来自一点点滴滴的积累; 3.广博让你更优秀,而专业让你无法替代; 4.挫折和失败能够转换为一种财富。

  • 889
    原创
  • 1
    翻译
  • 13
    转载
  • 24
    评论
  • 访问:1147873次
  • 积分:1523
  • 等级:核心会员
  • 排名:第6名
精华文章
    最新问题
    查看更多+
    热门文章
      热门用户
      推荐用户
        Copyright © 2016 All Rights Reserved. Powered by CNDBA · 皖ICP备2022006297号-1·

        QQ交流群

        注册联系QQ