Concatenating a query expression in Azure CLI - azure-cli

I'm trying to query a list of soft-deleted Azure key vaults, then determine if the name of a specific key vault is listed.
keyVaultName=whatever
az keyvault list-deleted --query "[?contains(name, $keyVaultName)]" -o json
That produces the output...
az keyvault list-deleted: error: argument --query: invalid jmespath_type value: '[?contains(name,whatever)]'
So apparently I need to wrap that variable name in single quotes - but how? Is there some sort of string concatenation function?

The contains return true or false, the format is
boolean contains(array|string $subject, any $search)
You could try this
kv=nancykeyvault
az keyvault list-deleted --query "contains([].name,'$kv')"
Or, just wrap the variable in single quotes like this, it by default outputs as the JSON format.
az keyvault list-deleted --query "[?contains(name,'$kv')]"

Related

including $ in search string for get_json_object

$ is used as root object for get_json_object. My json string has already $ in the name of json key, how can I extract it's value? I dont want to use json_tuple.
create external table testing_hive (records string);
insert into testing_hive values("{\"$num\":\"hey\"}");
select get_json_object(testing_hive.records, '$.$num') from testing_hive;
You can replace "$num" with something else without $ in it, for example "xx_num":
select get_json_object(regexp_replace(testing_hive.records,'\\"\\$num\\"','\\"xx_num\\"'), '$.xx_num') as num from testing_hive;
Result:
hey
Also you can replace $ for all keys with some other prefix in single regex_replace:
regexp_replace(testing_hive.records,'\\"\\$(.*?\\":)','\\"xx_$1')
I included ": in the pattern to make sure it will match keys only, not values. Use '$1' as a replacement instead of '\\"xx_$1' if you want to remove $ and leave key without $ as is.
Hope you got the idea. Modify regex pattern accordingly.

What is the best way to query the following file systems for a specific file system ID?

On my AWS account I have four file systems. In creating each of these file systems, I have given each on a tag Name. I would like to use the AWS CLI to filter out the one with a specific name, and retrieve its file system id.
I have already tried looking in JMESPath in order to filter out the one specific file system, but it seems the structure of my query as well as the structure of the output is a bit difficult for me to work with.
Here is my query so far:
aws efs describe-file-systems --query FileSystems[?Name=='dev-four'].FileSystemId --region us-east-1 --output text
Ideally, I would get the file system id as a single string, but I keep getting the error:
Bad value for --query FileSystems[?Name==dev-sg].FileSystemId: Bad jmespath expression: Unknown token '-':
FileSystems[?Name==dev-four].FileSystemId
I am not sure how to do the JMES a different way.
I think you should escape the query. Instead of using single-quotes around dev-four, try using escaped back-ticks. jmespath can be quirky about escape sequence.
--query FileSystems[?Name == \`dev-spring\`].FileSystemId
On my mac, I need the back slashes. Try the query with and without them, it may affect the result. More detail here

Mailchimp : Filtering data returned form a cURL request

I'm trying to make curl request to mailchimp in order to retrieve members informations. According to the doc, a special parameters name fields can be passed to the query in order to filter the result to the desired fields. See straight from the doc :
Query string parameters :
fields
Type: Array
Title: Fields
Read only: false
A comma-separated list of fields to return. Reference parameters of
sub-objects with dot notation.
The following data structure is returned on a sucessfull request :
{"members":[{"id":"106b60f0cdc8db34b7aae820d345ebd8","email_address":"blabla#freddiesjokes.com"
,"unique_email_id":"88e38b5109","email_type":"html","status":"subscribed",
"merge_fields":{"FNAME":"","LNAME":""} .... ]}
As you can see the email_address field is present. Therefore the following request always return an {} to me
curl --request GET \
--url 'https://us14.api.mailchimp.com/3.0/lists/744415ffa6/members?fields=email_adress' \
--user 'smth:apikey' \
--include
Is there something wrong with the request ? Without the fields parameters it correctly fetch the data. I see it's supposed to be an array, which, for me, means i could chain the fields parameters such as fields=blabla&fields=john&fields=doe do i stand correct on this ?
Thanks for you help
Look at: https://developer.mailchimp.com/documentation/mailchimp/guides/get-started-with-mailchimp-api-3/ which suggests you need to separate different fields with a comma and use the lists. prefix as in:
curl https://us14.api.mailchimp.com/3.0/lists?fields=lists.email_adress'

Tcl: spaces in command substitution

I have a function which outputs a string with or without spaces.
I want to set a variable to the function output. I use the following command:
set name [get_name_function object]
The problem is that if object name contains spaces (i.e. name with spaces), the whole name is putted in curly braces (name is equal to {name with spaces}).
How can I get the correct name?
It sounds like get_name_function is returning a Tcl list, not a string. You might want to use
set name [join [get_name_function object] " "]

How to use a pair of single-quotes inside double qoutes in bash?

I am going to write a bash script to manipulate user's data on mysql DB.
Here is the problem. I need to pass a variable's value into a Mysql query string:
read USERNAME;
echo "USE drupdb; SELECT uid FROM users WHERE name= '%USERNAME';" > /tmp/query.sql ;
Whatever combinations that I've used (including backslashs befor single-quotes to scape them) did not do the trick. I still get something other than the value of %USERNAME inside the query.sql.
I appreciate your hints.
You need to use $ to dereference a variable. Change %USERNAME to $USERNAME and everything should work fine:
read USERNAME;
echo "USE drupdb; SELECT uid FROM users WHERE name= '$USERNAME';"