linux 中 grep命令 $?返回值 0、1、2的意义

 

0:找到匹配模式

1:未找到匹配模式

2:指定的输入文件不对

001、

[root@pc1 test1]# ls
a.txt
[root@pc1 test1]# cat a.txt            ## 测试文本
aa bb cc
11 aa 33
33 dd bb
[root@pc1 test1]# grep "aa" a.txt      ## 匹配aa
aa bb cc
11 aa 33
[root@pc1 test1]# echo $?              ## 匹配到,返回0
0
[root@pc1 test1]# grep "xx" a.txt      ## 未匹配到xx,返回1
[root@pc1 test1]# echo $?
1
[root@pc1 test1]# grep "aa" b.txt      ## 指定的文件不对,返回2
grep: b.txt: No such file or directory
[root@pc1 test1]# echo $?
2
[root@pc1 test1]# grep "xx" b.txt
grep: b.txt: No such file or directory
[root@pc1 test1]# echo $?
2

 。

 

posted @ 2024-02-19 15:05  小鲨鱼2018  阅读(216)  评论(0编辑  收藏  举报