json 将 bash 变量传递给 jq select

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40027395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:39:19  来源:igfitidea点击:

Passing bash variable to jq select

jsonbashenvironment-variablesjq

提问by asidd

I have written a script to retrieve certain value from file.json. It works if I provide the value to jq select, but the variable doesn't seem to work (or I don't know how to use it).

我编写了一个脚本来从file.json. 如果我将值提供给 jq select,它会起作用,但该变量似乎不起作用(或者我不知道如何使用它)。

#!/bin/sh

#this works ***
projectID=$(cat file.json | jq -r '.resource[] | select(.username=="[email protected]") | .id')
echo "$projectID"

[email protected]

#this does not work *** no value is printed
projectID=$(cat file.json | jq -r '.resource[] | select(.username=="$EMAILID") | .id')
echo "$projectID"

回答by peak

Consider also passing in the shell variable (EMAILID) as a jq variable (here also EMAILID, for the sake of illustration):

还考虑传入 shell 变量 (EMAILID) 作为 jq 变量(这里也是 EMAILID,为了说明):

   projectID=$(cat file.json | 
     jq -r --arg EMAILID "$EMAILID" '
        .resource[]
        | select(.username==$EMAILID) 
        | .id')

Postscript

后记

For the record, another possibility would be to use jq's envfunction for accessing environment variables. For example, consider this sequence of bash commands:

作为记录,另一种可能性是使用 jq 的env函数来访问环境变量。例如,请考虑以下 bash 命令序列:

[email protected]  # not exported
EMAILID="$EMAILID" jq -n 'env.EMAILID'

The output is a JSON string:

输出是一个 JSON 字符串:

"[email protected]"

回答by asidd

I resolved this issue by escaping the inner double quotes

我通过转义内部双引号解决了这个问题

projectID=$(cat file.json | jq -r ".resource[] | select(.username==\"$EMAILID\") | .id")

回答by Gilles Quenot

It's a quote issue, you need :

这是一个报价问题,您需要:

projectID=$(
  cat file.json | jq -r ".resource[] | select(.username=='$EMAILID') | .id"
)

If you put single quotesto delimit the main string, the shell takes $EMAILIDliterally.

如果用单引号分隔主字符串,shell 会按$EMAILID字面意思处理。

"Double quote" every literal that contains spaces/metacharacters and everyexpansion: "$var", "$(command "$var")", "${array[@]}", "a & b". Use 'single quotes'for code or literal $'s: 'Costs $5 US', ssh host 'echo "$HOSTNAME"'. See
http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/Arguments
http://wiki.bash-hackers.org/syntax/words

“双引号”包含空格/元字符和每个扩展的每个文字:"$var", "$(command "$var")", "${array[@]}", "a & b". 使用'single quotes'代码或文字$'s: 'Costs $5 US'ssh host 'echo "$HOSTNAME"'。请参阅
http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/Arguments
http://wiki.bash-hackers.org/syntax/words

回答by Andrew Lockhart

Another way to accomplish this is with the jq "--arg" flag. Using the original example:

实现此目的的另一种方法是使用 jq "--arg" 标志。使用原始示例:

#!/bin/sh

#this works ***
projectID=$(cat file.json | jq -r '.resource[] | 
select(.username=="[email protected]") | .id')
echo "$projectID"

[email protected]

# Use --arg to pass the variable to jq. This should work:
projectID=$(cat file.json | jq --arg EMAILID $EMAILID -r '.resource[] 
| select(.username=="$EMAILID") | .id')
echo "$projectID"

See here, which is where I found this solution: https://github.com/stedolan/jq/issues/626

看到这里,这是我找到这个解决方案的地方:https: //github.com/stedolan/jq/issues/626

回答by Zaid

Posting it here as it might help others. In string it might be necessary to pass the quotes to jq. To do the following with jq:

在这里发布它,因为它可能会帮助其他人。在字符串中,可能需要将引号传递给 jq。使用 jq 执行以下操作:

.items[] | select(.name=="string")

in bash you could do

在 bash 你可以做

EMAILID=
projectID=$(cat file.json | jq -r '.resource[] | select(.username=='\"$EMAILID\"') | .id')

essentially escaping the quotes and passing it on to jq

基本上转义引号并将其传递给 jq

回答by Rodrigo Andrade

I know is a bit later to reply, sorry. But that works for me.

我知道有点晚才回复,抱歉。但这对我有用。

export K8S_public_load_balancer_url="$(kubectl get services -n ${TENANT}-production -o wide | grep "ingress-nginx-internal$" | awk '{print }')"

And now I am able to fetch and pass the content of the variable to jq

现在我可以获取变量的内容并将其传递给 jq

export TF_VAR_public_load_balancer_url="$(aws elbv2 describe-load-balancers --region eu-west-1 | jq -r '.LoadBalancers[] | select (.DNSName == "'$K8S_public_load_balancer_url'") | .LoadBalancerArn')"

In my case I needed to use double quote and quote to access the variable value.

在我的情况下,我需要使用双引号和引号来访问变量值。

Cheers.

干杯。

回答by DukeLion

Jq now have better way to acces environment variables, you can use env.EMAILI:

Jq 现在有更好的方法来访问环境变量,您可以使用 env.EMAILI:

projectID=$(cat file.json | jq -r ".resource[] | select(.username==env.EMAILID) | .id")

回答by markroxor

Little unrelated but I will still put it here, For other practical purposes shell variables can be used as -

一点不相关,但我仍然会把它放在这里,出于其他实际目的,shell 变量可以用作 -

value=10
jq  '."key" = "'"$value"'"' file.json