jq:传递不带引号的字符串参数 [英] jq: pass string argument without quotes

查看:315
本文介绍了jq:传递不带引号的字符串参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想传递一个不带引号的参数(默认情况下,JQ arg具有双引号),因为它应该用作过滤器.例如

I would like to pass an argument without quotes (JQ arg has double quotes by default) since it should be used as a filter. For e.g.

propt='.properties'
final=($(jq -r -c --arg p $propt '$p' sample.json))
echo $final

sample.json

sample.json

{
  "type": "object",
  "description": "Contains information",
  "properties": {
    "type": {
      "description": "Type"
        }
   }
}

因此最终它会打印出.properties而不是预期的{"type":{"description":"Type"}} 我为此使用bash外壳.

So ultimately it prints out .properties instead of the expected {"type":{"description":"Type"}} I use a bash shell for this purpose.

请让我知道我在做什么错.

Please let me know what I am doing wrong.

推荐答案

如果我对您的理解正确,那么您就会以为需要在jq中设置变量,而不仅仅是让Shell执行扩展:

If I understand you correctly, you're getting sidetracked by thinking you need to set up a variable in jq, instead of just letting the shell do an expansion:

% foo='.properties'
% jq -r -c  "$foo" sample.json 

输出:

{"type":{"description":"Type"}}

请注意$ foo上的双引号仍然允许外壳程序将变量扩展为.properties.也就是说,您可能不安全地使用:jq -r -c $foo sample.json

Note the double quotes on $foo to still allow the shell to expand the variable to .properties. That said you could unsafely use: jq -r -c $foo sample.json

这篇关于jq:传递不带引号的字符串参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆