shell – 终端 – 为什么即使找不到匹配项,grep的exit命令也是0
我有这个命令:
grep -E '^nothing' List.txt | echo $? 这里grep与任何东西都不匹配,我只是输出它的退出代码.根据grep的文档:
但: prompt:user$grep -E '^nothing' List.txt | echo $? 0 prompt:user$ 但是,即使匹配不存在,为什么我得到0作为输出,我不应该得到预期的1退出代码吗? 解决方法
这就是问题:
grep -E '^nothing' List.txt | echo $? 通过使用单个|你正在发送grep的输出到echo,它总是打印上一个命令的退出状态,无论是否找到模式,它总是为0. 你可以使用grep -q: grep -qE '^nothing' List.txt 按照男人的说法: -q,--quiet,--silent Quiet mode: suppress normal output. grep will only search a file until a match has been found,making searches potentially less expensive. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |