I make some testing below:
---------------------------------------------------------------------------------------------
$ ls
aa.txt ab.txt bb.txt
$ ls | grep -v 'aa.txt' | grep -v 'bb.txt'
ab.txt
$ echo $?
0
$ ls | grep -v 'aa.txt' | grep -v 'bb.txt' |grep -v 'ab.txt'
$ echo $?
1
$ ls | grep -E -v '(aa.txt|bb.txt|ab.txt)'
grep: illegal option -- E
Usage: grep -hblcnsviw pattern file . . .
$ ls | /usr/xpg4/bin/grep -E -v '(aa.txt|bb.txt|ab.txt)'
$ echo $?
1
$ ls abc.txt
abc.txt: No such file or directory
$ echo $?
2
--------------------------------------------------------------
From the testing above, we can find that if ls can’t find certain file,the return value will be 2,and display some error message, that is this command failed. If we use ls without parameters and there are no files in the current diretory, ls will return 0, that is this command successfully be executed.
But grep works in another way. According to the pattern grep will filter some lines. if no line get thought, the return value is 1, if some lines get thought, the return value is 0. so the return value of grep is 1 not mean grep fail, but no line get thought.
本文通过一系列测试案例,详细解析了Linux环境下ls和grep命令的返回值规律及其含义。当ls找不到指定文件时,返回值为2;若当前目录为空,则返回0。而grep根据是否匹配到行来决定其返回值,未找到匹配项时返回1,反之则返回0。
2万+

被折叠的 条评论
为什么被折叠?



