Friday, October 7, 2022

[SOLVED] How to store dynamic value in Variable For Bash?

Issue

I have below command:

ExpirationDate=$(date -d '+60 days' +'%Y-%m-%d')
VaultName="abc"
getapp=$(az keyvault secret list --vault-name $VaultName --query "[].{SecretName:name,ExpiryDate:attributes.expires} [?ExpiryDate<='$ExpirationDate']" | jq '.[].SecretName' | tr -d '"')

getserviceprincipal=$(az keyvault secret list --vault-name $VaultName --query "[].{Type:contentType,ExpiryDate:attributes.expires} [?ExpiryDate<='$ExpirationDate']" | jq '.[].Type' | tr -d '"')
     
## get length of $distro array
len=${#getapp[@]}

## Use bash for loop 
for (( i=0; i-le$len-1; i++ ))
 do 
 echo "${getapp[$i]}" 
  ./resetpassword.sh -a ${getapp[$i]} -s  ${getserviceprincipal[$i]} -y
 echo "${getserviceprincipal[$i]}" 
 done

in this command I want store all value of vault name getapp and similarly getserviceprincipal. Example If I have more then 2 vault in getapp variable then script is not working due to $getapp is not storing variable in array.

Is anyone help me to put out this simple solutions!! Thanks In Advance..


Solution

readarray -t getapp < <( az keyvault ... | tr -d '"' ) should do the trick here.

Note that this requires newlines to be valid delimiters. If there can be newlines in your data then you'll have to pick a different delimiter with the -d delim option. If there isn't any single delimiter that works everywhere then bash may not be the best choice for this.



Answered By - tjm3772
Answer Checked By - Clifford M. (WPSolving Volunteer)