0

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.

Zhulian Ginev
  • 141
  • 3
  • 14
  • Put `$proc_check` in quotes, just like you do in the first script. – Barmar Aug 22 '19 at 18:53
  • Just saw that you said you tried that. You should post that version. – Barmar Aug 22 '19 at 18:55
  • The problem is probably a difference in whitespace. – Barmar Aug 22 '19 at 18:56
  • Did you quote the variable when you piped it to `od -c`? Show the output of `echo "$proc_check" | od -c` – Barmar Aug 22 '19 at 18:56
  • Thanks but I've already tried that, with or without quotes on $proc_check, double or single quotes on the string, as well as double or single brackets on the if statement. And I've mixed those in various ways, it just wouldn't work. – Zhulian Ginev Aug 22 '19 at 18:57
  • Please run the script with `set -x` and post the resulting debug log – that other guy Aug 22 '19 at 18:57
  • @thatotherguy I've added the debug run in the question – Zhulian Ginev Aug 22 '19 at 19:19
  • 1
    Well, there you go. Your string has linefeeds in it but you compare it to a string without linefeeds. See [this question](https://stackoverflow.com/questions/29378566/i-just-assigned-a-variable-but-echo-variable-shows-something-else) for why `echo` doesn't show linefeeds – that other guy Aug 22 '19 at 20:59

1 Answers1

0

Do you need to match the whole string? If you only need the "Enable succeeded" part, maybe cut off everything after the ":" character (use bash parameter expansion magic "%" operator).

if [ "${proc_check%%:*}" = 'Enable succeeded' ]; then

Update: I saw the debugging output you added, looks like proc_check has newlines that aren't in your comparison string. I think cutting off the extra text like I suggest is the best way.

Another update: Code for the follow-up comment I made, if the important text is "Proc is running" you can match it like this:

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 '[]' | grep -o "Proc is running")

If it's not there, the text will be empty. So you can check it like this:

if [ "$proc_check" = 'Proc is running' ]; then
John Bayko
  • 746
  • 4
  • 7
  • That doesn't work for me, to be frank "Enable succeeded" is the only part I don't really need. This is also not a solution on my question though, I want to be able to use the whole string. – Zhulian Ginev Aug 22 '19 at 19:20
  • 1
    Okay, well maybe pipe the output of the command through tr and replace the newlines with spaces. Or maybe "grep -o" to extract the exact string you're looking for. – John Bayko Aug 22 '19 at 19:22
  • 1
    Thanks mate, stripping the newlines worked. 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. – Zhulian Ginev Aug 22 '19 at 19:42