Quantcast
Channel: Active questions tagged jq - Stack Overflow
Viewing all articles
Browse latest Browse all 657

How to catch "$variable is not defined" in jq?

$
0
0

Let's pretend I'm running something like this:

jq -nr --arg target /tmp \'(["echo","Hello, world"]|@sh)+">\($target)/sample.txt"' \| sh

Everything is fine unless I forgot to pass variable $target:

$ jq -nr '(["echo","Hello, world"]|@sh)+">\($target)/sample.txt"'jq: error: $target is not defined at <top-level>, line 1:(["echo","Hello, world"]|@sh)+">\($target)/sample.txt"jq: 1 compile error

How can I catch this and use default value?

I've tried:

  1. $target?
  2. ($target)?
  3. try $target catch null
  4. $target? // null

But it seems to be parsing-time error, which obviously can't be caught at runtime. Have I've missed any dynamic syntax?

I've found that command-line arguments can be found in $ARGS.name, but there are two drawbacks:

  1. This was introduced in version 1.6, but I have 1.5 on CentOS 7.
  2. It doesn't catch locally defined variables.

Viewing all articles
Browse latest Browse all 657

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>