I'm using Jenkins to deploy a bash script, which then executes several other scripts onto a Azure VM (using the Azure CLI). I'm trying to use command substitutions on the azure cli calls, so that I can create conditions in the script based on the received output, but I cannot seem to compare the command substituted variables to a string.
I also have the following condition before that to login to azure with the proper credentials, which I have no problem with, but I parse it in a way where the command substitution only returns "Enabled", so I have this:
echo "Waiting for credentials to be initialized..."
auth_init=$(az login --service-principal -u $azure_client_id -p $azure_client_secret -t $azure_tenant_id --query [].state -o tsv)
sleep 10
if [ "$auth_init" != 'Enabled' ]; then
echo "Credentials couldn't be initialized, aborting..." | tee -a "$errordir/error.log"
exit 2
else
echo "Credentials initialized successfully!"
fi
az account set -s $azure_subscription_id
echo "Subscription set to $azure_subscription_id"
Which works without an issue. And then I have the next part:
echo "Executing Proc Health Check Script..."
proc_check=$(az vm run-command invoke -g $azure_vm_rg -n $azure_vm_hostname --query value[0].message -o tsv --command-id RunShellScript --scripts "@health_check_proc.sh" | tr -d '[]')
sleep 20
echo "Execution results are:"
echo $proc_check
if [ "$proc_check" = 'Enable succeeded: stdout Proc is running, waiting for installation to finish... stderr' ]; then
echo "Proc Health Check is successful, proceeding with installation health check..."
else
echo "Proc Health Check failed, aborting..." | tee -a "$errordir/error.log"
exit 2
fi
But whatever I do, even though I echo the output from proc_check and it looks absolutely the same as the string in my condition, they just wouldn't match in the if statement. Please do bear in mind that I've already tried both = and == for comparing, both single and double brackets for the conditional, both single or double quoting the variable and/or the string. And I've also thought that maybe the output from azure isn't exactly the same, as it might have tab separation instead of my spaces, so I compared it with another variable with my string using od -c and they look exactly the same.
EDIT: Here's the failing part with ran with set -x:
+ az vm run-command invoke -g *** -n *** --query 'value[0].message' -o tsv --command-id RunShellScript --scripts @health_check_proc.sh
+ tr -d '[]'
+ proc_check='Enable succeeded:
stdout
Proc is running, waiting for installation to finish...
stderr'
+ sleep 20
+ echo 'Execution results are:'
Execution results are:
+ echo Enable succeeded: stdout Proc is running, waiting 'for' installation to finish... stderr
Enable succeeded: stdout Proc is running, waiting for installation to finish... stderr
+ '[' 'Enable succeeded:
stdout
Proc is running, waiting for installation to finish...
stderr' '=' 'Enable succeeded stdout Proc is running, waiting for installation to finish... stderr' ]
+ echo 'Proc Health Check failed, aborting...'
+ tee -a /var/jenkins_home/...
Proc Health Check failed, aborting...
+ exit 2
SOLUTION: For people struggling with the same, I piped $proc_check to: tr -d '\n' which removes the newlines and then the returned output became "Enable succeeded: stdoutProc is running, waiting for installation to finish...stderr", but hey, I can work with that.