0

I am trying to run an Azure CLI command inside a shell script and unable to resolve the below issue.

Operation I am trying to do : Delete all resource groups starting with 'test' within a subscription. In this regard when I execute the below commands for deleting the resource group individually outside of the script, it works fine :

az login
rg='test.example1'
az group delete -g $rg --subscription <sub-id> -y

However, when I try to do the below inside the shall script, I am getting a validation error : ValidationError: Parameter 'g' must conform to the following pattern: '^[-\w\._\(\)]+$'.

This is my shell script snippet :

for rg in $(az group list --subscription <sub-id> --query [].name); do
   if [[ "$rg" =~ test* ]];then
      echo "Deleting resource group : $rg"
      az group delete -g $rg --subscription <sub-id> -y
   fi
done 

The echo is showing the resource group as expected within quotes, like

Deleting resource group :"test.example1"

I have a hunch that the $rg in the az group delete command is not being read correctly. However, I am unable to figure out how I can fix that.

Update : Added the screenshot @CharlesXu. Please note I added the break to limit the deletion to just one resource group during testing. enter image description here

4
  • The line rg = 'test.example1' does not assign a value to rg. I would suspect you get an error message from that line. Commented Dec 14, 2020 at 23:53
  • Updated. It was a typo with the space around =. Removed that from the snippet above Commented Dec 15, 2020 at 0:02
  • Side problem: the test in if [[ "$rg" =~ test* ]] probably isn't doing what you want -- it'll match if the value contains "tes" anywhere. This is because =~ does regular expression matching, and the regex test* matches "tes" followed by zero or more "t" characters (and the match isn't anchored, so it'll match anywhere in the string). You probably want a glob pattern match instead; to get that, use = instead of =~. Commented Dec 15, 2020 at 2:43
  • I guess thats not a problem because the echo inside the if block is printing what I expect it to print . I mentioned that in the OP as well. echo "Deleting resource group : $rg" Commented Dec 15, 2020 at 4:14

2 Answers 2

1

Your question is all about quotes. If echo is printing the line:

Deleting resource group :"test.example1"

that indicates that the variable rg has double quotes in it. When you call:

az group delete -g $rg --subscription <sub-id> -y

it is the same as if you called:

az group delete -g '"test.example1"' --subscription <sub-id> -y

which is almost certainly not what you want. You need to get the quotes out of the variable. A simple (but arguably terrible) approach is to use eval. A better approach would be to modify the call to az so that it does not emit the quotes. Another approach would be to use a bash extension and write:

az group delete -g "${rg//\"/}" --subscription <sub-id> -y

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! But using the above command is still throwing the same error.
@user5566364 Try putting set -x before the relevant section, so you can see what the shell thinks is going on.
0

I think the output you have seen should like this:

Deleting resource group :"test.example1",

And I can reproduce it:

enter image description here

The problem is that the element of the list for group names come with ,. So you need to command that get the list of group names into this:

az group list --subscription <sub-id> --query [].name -o tsv

And then it works fine and like this:

enter image description here

I do not want to delete the groups, so I change the command to show them as examples.

Update:

To filter the group with the test prefix, you can do it when you get the group names in the list:

az group list --subscription <sub-id> --query [].name -o tsv | grep '^test'

Then you do not need the if condition.

9 Comments

Tried this, but no luck.
@user5566364 The same error? Can you share the screenshot?
Added the screenshot. Please note - I have intentionally added the break statement to limit the damage( if any) during testing.
@user5566364 The same error as I reproduce. And I cannot see the whole group name you display. Can you share the screenshot without hiding the name after "Deleting resource group :"?
Updated with the requested screenshot
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.