If statement &&;尝试grep多个文件时被忽略

If statement &&;尝试grep多个文件时被忽略,if-statement,grep,ignore,If Statement,Grep,Ignore,我正在尝试grep在几个不同的文件,使用不同类型的匹配。但出于某种原因,它似乎忽略了&&后的参数 $ grep -E -i "err|warn" h2_backups.log storagefolder_backups.log $ grep -E -i 'err|warn' exports.log imports.log exports.log:err $ grep -E -i "err|warn" h2_backups.log storagefolder_backups.log &&a

我正在尝试grep在几个不同的文件,使用不同类型的匹配。但出于某种原因,它似乎忽略了&&后的参数

$ grep -E -i "err|warn" h2_backups.log storagefolder_backups.log
$ grep -E -i 'err|warn' exports.log imports.log
exports.log:err
$ grep -E -i "err|warn" h2_backups.log storagefolder_backups.log && grep -E -i 'err|warn' exports.log imports.log
$ grep -E -i 'err|warn' exports.log imports.log && grep -E -i "err|warn" h2_backups.log storagefolder_backups.log
exports.log:err
如您所见,第一个grep不包含错误,但第二个grep包含错误。在第三行中,我首先使用了grep,没有错误匹配,这会忽略&&&之后的所有内容。在第四行,我已经切换了greps的位置,现在我得到了一个匹配项(但是它可能仍然忽略了&&&之后的所有内容)


在我的其他服务器上使用&&似乎有效。有什么想法可能导致这种情况吗?

根据短路评估,这是正常行为。您正在执行
command1和&command2
。如果
command1
返回False,则不会计算
command2
,因为整个条件已经为False


请参见一个非常简单的示例:

$ cat a
hello
grep“hello”
返回一些内容,而
grep“bye”
不会:

$ grep "hello" a
hello
$ grep "bye" a
$
因此,添加
时的行为&echo“yes”


值得一提的是,这被称为“短路评估”。
$ grep "hello" a && echo "yes"
hello
yes
$ grep "bye" a && echo "yes"
$