summaryrefslogtreecommitdiff
path: root/src/journal
diff options
context:
space:
mode:
authorFrantisek Sumsal <frantisek@sumsal.cz>2019-05-15 19:28:09 +0200
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2019-05-16 18:54:01 +0200
commit6cda6774bdd90f8496d38dcd75d9be8a12660577 (patch)
treeddd059f93270f1ea9bb41159af6a2cb22649b270 /src/journal
parent8688c29b5aece49805a244676cba5bba0196f509 (diff)
downloadsystemd-6cda6774bdd90f8496d38dcd75d9be8a12660577.tar.gz
systemd-6cda6774bdd90f8496d38dcd75d9be8a12660577.tar.bz2
systemd-6cda6774bdd90f8496d38dcd75d9be8a12660577.zip
journalctl: return a non-zero EC when --grep returns no matches
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
Diffstat (limited to 'src/journal')
-rw-r--r--src/journal/journalctl.c10
1 files changed, 9 insertions, 1 deletions
diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
index 88ee4ee35f..91a21e407f 100644
--- a/src/journal/journalctl.c
+++ b/src/journal/journalctl.c
@@ -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;