]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journalctl: return a non-zero EC when --grep returns no matches
authorFrantisek Sumsal <frantisek@sumsal.cz>
Wed, 15 May 2019 17:28:09 +0000 (19:28 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 16 May 2019 16:54:01 +0000 (18:54 +0200)
When journalctl is compiled with PCRE2 support, let's return a non-zero
exit code when --grep is used and no match for given pattern is found.
This should allow users to use journalctl --grep in scripts instead of
piping journalctl into grep

Fixes #8152

src/journal/journalctl.c

index 88ee4ee35f69c99edee3e53b08b021401415708f..91a21e407fcedab0595da0719d13b975c35c1468 100644 (file)
@@ -2678,8 +2678,16 @@ finish:
         free(arg_verify_key);
 
 #if HAVE_PCRE2
-        if (arg_compiled_pattern)
+        if (arg_compiled_pattern) {
                 pcre2_code_free(arg_compiled_pattern);
+
+                /* --grep was used, no error was thrown, but the pattern didn't
+                 * match anything. Let's mimic grep's behavior here and return
+                 * a non-zero exit code, so journalctl --grep can be used
+                 * in scripts and such */
+                if (r == 0 && n_shown == 0)
+                        r = -ENOENT;
+        }
 #endif
 
         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;