How to use dynamic variables in expression
myres.txt
[
{
"name": "test-001",
"is_preconfigured": true,
},
{
"name": "my-connector",
"is_preconfigured": 3
},
{
"name": "checkerfrom_default2_002",
"is_preconfigured": false,
}
]
When i use direct command
yq e '. | .[].name | select(. == "my-connector")' myres.txt
This gives me result as my-connector
But when I use following way NAME_IN_YAML="my-connector"
EXISTING_NAME=$(yq e '. | .[].name | select(. == $NAME_IN_YAML)' myres.txt)
This is blank result
CodePudding user response:
With mikefarah/yq use the env(..)
function to load environment variables from the shell, i.e.
NAME_IN_YAML="my-connector" yq e '.[].name | select(. == env(NAME_IN_YAML))' myres.txt
Note that, if you are using yq
version above 4.18.1, the eval action e
is the default one and can be skipped altogether.
If you are looking to format/manipulate JSON text, you should probably consider using jq which has loads of features to use.