0

I'm trying to get the value of a resource in azure via AZ CLI, and pass that value to a variable in bash.

id=$(az synapse workspace show -n $name -g $rsname --query 'identity.principalId' -o tsv 2<&1)
if [[ $id == *"Not Found"* ]];
then
    echo "Workspace already deleted."
fi

If the resource is not there, I am redirecting the output to the variable with 2<&1 so I can deal with it in the if-then conditional. $id is getting assigned the output correctly, but AZ CLI is still exiting the script with error "not found".

Is there anyway to keep it from exiting?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
egrok
  • 55
  • 4

1 Answers1

0

In your bash command you are using 2<&1 that's why it exited the script with error "not found"

You can achieve this by using "2>&-".

Make sure you have to use the Greater than (>) symbol.

    id=$(az synapse workspace show -n '<synapse Name>' -g '<Resource Group Name>' --query 'identity.principalId' -o tsv 2>&-)

using "2>&-":

I can be able to get the principal id. enter image description here

using "2>&1":

here I am not able to get the principal id. enter image description here

Reference

Delliganesh Sevanesan
  • 4,146
  • 1
  • 5
  • 15