Issue
Im trying to grep in several different files, using different type of matching. But for some reason it seems it ignores the arguments after &&.
$ 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
As you can see, the first grep contains no error but the second grep does. On the third line I used the grep with no error match first, and this ignores everything after &&. On the forth line I've switched places of the greps and now I get a match (however it probably still ignores everything after &&).
Using && seems to work on my other servers. Any thoughts what might cause this?
Solution
This is the normal behaviour as per the short-circuit evaluation. You are doing command1 && command2
. If command1
returns False, then command2
will not be evaluated because the whole condition is already False.
See in a very simple example:
$ cat a
hello
grep "hello"
returns something, while grep "bye"
does not:
$ grep "hello" a
hello
$ grep "bye" a
$
Hence the behaviour when adding && echo "yes"
:
$ grep "hello" a && echo "yes"
hello
yes
$ grep "bye" a && echo "yes"
$
Answered By - fedorqui Answer Checked By - David Marino (WPSolving Volunteer)