From 4104c4fa1cef1fc422597177058c8eb5a6b3d4e0 Mon Sep 17 00:00:00 2001 From: Assaf Gordon Date: Fri, 6 Mar 2015 18:42:16 -0500 Subject: [PATCH] exit with non-zero code on runtime exceptions With this change, runtime exceptions are propagated to non-zero exit code of 'jq', allow better scripting and automation. The new exit code value is 5. This allows using the shell's and/or operations ('&&' and '||') to detect input runtime exceptions. Before: runtime exceptions are printed to STDERR, but not reported as non-zero exit-code: $ echo '"hello"' | jq '.|tonumber' ; echo $? jq: error: Invalid numeric literal at EOF at line 1, column 5 (while parsing 'hello') 0 After: $ echo '"hello"' | ./jq '.|tonumber' ; echo $? jq: error: Invalid numeric literal at EOF at line 1, column 5 (while parsing 'hello') 5 Note that there's a subtle interplay when using "-e" option. The value of the non-zero exit code changes from 4 to 5, but it still indicates a 'failure' or non-true value. Before: $ echo '"hello"' | jq -e '.|tonumber' ; echo $? jq: error: Invalid numeric literal at EOF at line 1, column 5 (while parsing 'hello') 4 After: $ echo '"hello"' | ./jq -e '.|tonumber' ; echo $? jq: error: Invalid numeric literal at EOF at line 1, column 5 (while parsing 'hello') 5 --- main.c | 1 + 1 file changed, 1 insertion(+) diff --git a/main.c b/main.c index 8e46b29..adc9409 100644 --- a/main.c +++ b/main.c @@ -132,6 +132,7 @@ static int process(jq_state *jq, jv value, int flags, int dumpopts) { msg = jv_dump_string(msg, 0); fprintf(stderr, "jq: error (not a string): %s\n", jv_string_value(msg)); } + ret = 5; jv_free(msg); } jv_free(result); -- 2.40.0