Jeya Kumar
Jeya Kumar

Reputation: 1102

JQ return exit boolean instead of exit status code

I have following script

grep -e . testFile |
jq -e ".fundingMoved.\"com.test\".map.\"java.util.HashMap\" |

if .TRANSACTION_DESTINATION? then
(.AGREEMENT_OWNING_ORG | startswith(\"MXN\")) and (.TRANSACTION_DESTINATION |startswith(\"BRL\")) or
(.AGREEMENT_OWNING_ORG | startswith(\"MXN\")) and (.TRANSACTION_DESTINATION |startswith(\"CAD\")) or
(.AGREEMENT_OWNING_ORG | startswith(\"MXN\")) and (.TRANSACTION_DESTINATION |startswith(\"USD\"))
else
(.AGREEMENT_OWNING_ORG | startswith(\"USD\")) and (.WAREHOUSE |startswith(\"AUD\")) or
(.AGREEMENT_OWNING_ORG | startswith(\"USD\")) and (.WAREHOUSE |startswith(\"AED\")) or
(.AGREEMENT_OWNING_ORG | startswith(\"USD\")) and (.WAREHOUSE |startswith(\"EGP\"))

end"

If testFile has match then i am seeing true and if no match then i am seeing false

I am using this script in some legacy system so it is taking only grep as argument so i am using grep along with jq.

Even though jq return false, somehow grep is interpreting that as match and perform action.

I tried with -e still i am seeing true/false instead of exit status

what i am trying to do ?

My grep should return right status code.

If my condition is true in Jq then my grep should success and if my jq return false then my grep exit code should be non zero.

How do i return exit status code from jq instead of true/false. Currently jq is returning true or false hence grep is considering everything as match.

Upvotes: 2

Views: 5139

Answers (1)

ikegami
ikegami

Reputation: 386541

How do i return exit status code from jq instead of true/false.

This is what the -e command line switch it used.

-e / --exit-status:

Sets the exit status of jq to 0 if the last output values was neither false nor null, 1 if the last output value was either false or null, or 4 if no valid result was ever produced. Normally jq exits with 2 if there was any usage problem or system error, 3 if there was a jq program compile error, or 0 if the jq program ran.

Since the specific test is irrelevant, I'm going to use the simpler type == "string" test in my examples.

$ jq -e 'type == "string"' <<<'"abc"'
true

$ echo $?
0

$ jq -e 'type == "string"' <<<'123'
false

$ echo $?
1

Note that while the documentation suggests that jq -e exits with 4 when no input is provided, it actually exits with 0.

$ jq -e 'type == "string"' </dev/null

$ echo $?
0

But it's simple to change that by using -n and inputs.

$ jq -en 'inputs | type == "string"' <<<'"abc"'
true

$ echo $?
0

$ jq -en 'inputs | type == "string"' <<<'123'
false

$ echo $?
1

$ jq -en 'inputs | type == "string"' </dev/null

$ echo $?
4

Redirect the output (e.g. using >/dev/null) if you don't want to see the output.


halt_error also allows you to set the exit code, and it gives more control over the exit code produced. But it's more complicated to use, and it suffers from the same problem with empty inputs, and it doesn't have a simple workaround.

jq -s '
   if length == 0 then
      null | halt_error(1)
   else
      .[] |
      if type == "string" then
         null | halt_error(0)
      else
         null | halt_error(1)
      end
   end
'

Upvotes: 7

Related Questions