让我们假设我正在运行这样的东西:
jq -nr --arg target /tmp \
'(["echo","Hello, world"]|@sh)+">\($target)/sample.txt"' \
| sh
Run Code Online (Sandbox Code Playgroud)
一切都很好,除非我忘记传递变量$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
Run Code Online (Sandbox Code Playgroud)
我怎样才能捕捉到这个并使用默认值?
我试过了:
$target?($target)?try $target catch null$target? // null但是好像是解析时的错误,运行时显然是抓不到的。我错过了任何动态语法吗?
我发现可以在 中找到命令行参数$ARGS.name,但有两个缺点:
jq ×1